Tag Archives: strings

String concatenation performance in Java

There are (at least) three ways to concatenate several String objects in Java. Given the strA and strB String objects, those three ways are:

  1. strA + strB
  2. strA.concat(strB)
  3. new StringBuilder(strA).append(strB)

What’s the difference? In this case, not much. The + operation internally calls to StringBuilder.append, and all is about creating char arrays (char[]). The real problem here is working with String literals (instead of variables). Apparently StringBuilder works better with Java String Pool, so it finally creates only one String object to build the entire concatenation. On the other hand, + and concat create a new String every time they are called. So:

String str = "Hello, " + "my " + "name " + "is " + "Javier.";

internally creates ¿4? ¿5? String objects to actually build one, leaving almost every one of them waiting for the garbage collector. However

String str = new StringBuilder("Hello ").append("my ").append("name ")
    .append("is ").append("Javier.").toString();

just creates the String object we want to use.

It’s not a big deal, is it? Let’s change it to:

for (int i=0; i<100; i++) {
    String str = "Hello, " + "my " + "name " + "is " + "Javier.";
    //Do some stuff with str
}

Now we have lots of variables waiting for the garbage collector.

Maybe all of this is not that important… it’s possible that the people from Sun Oracle created a very smart compiler that optimizes those operations.

Bonus: How about String.format?

String.format is not exactly about concatenation. It lets you insert values inside a String object, by replacing placeholders. For example, we can insert a String value:

String name = "Javier";
String str = String.format("Hello, my name is %s.", name);

str should be equals to “Hello, my name is Javier.” We can achieve this result by using any of the concatenation techniques, like:

String name = "Javier";
String str = "Hello, my name is " + name + ".";

From a performance point of view, it probably would be faster by concatenating. String.format parses the original String object, looking for placeholders, and those kind of operations aren’t cheap in strings.

But sometimes I prefer it: It is more readable (for me) and it provides you a syntax to specify the formatting of the value (like precision for a decimal number value).

Anyway, I think it’s a matter of personal taste.

Update: It happened in a Real WorldTM project

My fellow ghackers1 friends discussed this topic just a couple of days this post was written. Andrés had to face some performance-sucker legacy code2; it was a String concatenation inside a nested loop… Concatenations were being executed thousands of times. The execution time changed from minutes to just 2 seconds by replacing all concatenations with StringBuilder.append. Amazing!

  1. It is not a mistake, it’s the name of our coders Mailing List. []
  2. It was probably written by me, oops. []