DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > We Don't Need StringBuilder for Simple Concatenation

We Don't Need StringBuilder for Simple Concatenation

Concatenating strings is useful, but expensive. Fortunately, you don't need to use StringBuilder anymore - the compiler can handle it for you.

A N M Bazlur Rahman user avatar by
A N M Bazlur Rahman
·
Dec. 22, 16 · Java Zone · Tutorial
Like (44)
Save
Tweet
182.69K Views

Join the DZone community and get the full member experience.

Join For Free

String concatenation has always been a well-discussed topic among Java developers. It's been costly.

Let's first dig into the issue of why it's costly.

In Java, string objects are immutable, which means once it is created, you cannot change it. So when we concatenate one string with another, a new string is created, and the older one is marked for the garbage collector.

string object an immutable

Let's say we need to concatenate a million strings. Then, we are creating 1 million extra strings which will eventually be garbage collected.

In string concatenation, under the hood, several operations occur. Using the dot operator to concatenate string is equivalent to using the String#concat(String) method.

public String concat(String str) {
    int otherLen = str.length();
    if (otherLen == 0) {
        return this;
    }

    int len = value.length;
    char buf[] = Arrays.copyOf(value, len + otherLen);
    str.getChars(buf, len);

    return new String(buf, true);
}
public static char[] copyOf(char[] original, int newLength) {
    char[] copy = new char[newLength];
    System.arraycopy(original, 0, copy, 0,
                     Math.min(original.length, newLength));
    return copy;
}
void getChars(char dst[], int dstBegin) {
    System.arraycopy(value, 0, dst, dstBegin, value.length);
}


As you can see, a new array of characters is created with a size of the length of the existing value plus the size of value that is appended. Then, their value is copied to the new array instance. Finally, a new String object is created from the array and returned.

So it's a lot of operations, which ends up being O(n^2) complexity — if you really want to calculate this issue out.

To solve this problem, the StringBuilder class is used. It works like a mutable String object. The append method helps to avoid all the copying required in string concatenation. It has O(n) complexity, which is better than O(n^2).

However, Java does this string concatenation using StringBuilder by default for the simple cases..

From the Java specifications:

To increase the performance of repeated string concatenation, a Java compiler may use the StringBuffer class or a similar technique to reduce the number of intermediate String objects that are created by evaluation of an expression.

Well, the Java compiler does that.

public class StringConcatenateDemo {
  public static void main(String[] args) {
     String str = "Hello ";
     str += "world";
   }
}


The above code will produce the following bytecode:

public class StringConcatenateDemo {
  public StringConcatenateDemo();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: ldc           #2                  // String Hello
       2: astore_1
       3: new           #3                  // class java/lang/StringBuilder
       6: dup
       7: invokespecial #4                  // Method java/lang/StringBuilder."<init>":()V
      10: aload_1
      11: invokevirtual #5                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      14: ldc           #6                  // String world
      16: invokevirtual #5                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      19: invokevirtual #7                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      22: astore_1
      23: return
}


As you can see in the bytecode, StringBuilder is used. So we don't need to use StringBuilder anymore in Java.

However, This is true for the simple cases.  If you need to concatenate inside the loop, it is always suggested to use  StringBuilder. 


TL; DR

For simple string concatenation, you need not use StringBuilder, java compiler will do the trick for you. However, if you need to concatenate inside a loop,  you need to manually apply StringBuilder.

For better understanding please go to this link. 


Edit:  The earlier I made this case with only Java 8. Turns out I was wrong. So I stand correct myself. It has nothing to do with java 8. 

Strings Data Types Java (programming language) Java compiler

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 3 Predictions About How Technology Businesses Will Change In 10 Years
  • Java: Why Core-to-Core Latency Matters
  • Growth in Java Development for Web and Mobile Apps
  • How To Integrate Third-Party Login Systems in Your Web App Using OAuth 2.0

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo