Improve the speed of string concatenation

String concatenation in Java is like a paper chain

You often use the Java concatenation operator (+) to concatenate (join) two strings together. And you use it with both string literals and string variables. But what’s going on behind the scenes?

String concatenation of literals

When you concatenate two string literals, the compiler creates a single string literal. That means:

String greeting = "Hello, " + "World";


String greeting = "Hello, World";

String concatenation with variables

When you concatenate a String reference and a literal (or another variable type), something different happens.

String louder = greeting + "!";


String louder = (new StringBuilder()).

What just happened?

Here’s what actually happens:

  • The compiler generates code to create a new StringBuilder object, using the default StringBuilder constructor. The constructor only allocates space for 16 chars.
  • The append() method is called. It appends the contents of the original string to the StringBuilder object.
  • The append() method is called again. It now appends the value on the right hand side of the + sign.
  • Finally the entire StringBuilder is converted back to a new String object by calling the toString() method.

So the following:

String s = "Joe";
s = s + " Bloggs";

actually gets converted to:

String s = "Joe";
s = new StringBuilder().append(s).
append(" Bloggs").toString();

See the problem?

This is a CPU-intensive process. Why? Because a number of temporary objects are created and then later garbage-collected. And a number of extra methods are called which internally copy characters one at a time to the new object.

The solution

You can see that it is more efficient to create a StringBuilder with the required size. Don’t rely on the default constructor. Append everything by calling the append() method as many times as you need. Then do a single toString() conversion.

Of course, you don’t always need to do this. But if you have code that is frequently called and that contains a lot of string concatenation, this will definitely improve the speed.

I’m always interested in your opinion, so please leave a comment.

1 thought on “Improve the speed of string concatenation”

  1. Ntethelelo Zikalala

    Great insights, I will definitely take this into consideration next time I work with strings. Thanks for the tip!.

Leave a Comment

Your email address will not be published. Required fields are marked *

Code like a Java Guru!

Thank You

We're Excited!

Thank you for completing the form. We're excited that you have chosen to contact us about training. We will process the information as soon as we can, and we will do our best to contact you within 1 working day. (Please note that our offices are closed over weekends and public holidays.)

Don't Worry

Our privacy policy ensures your data is safe: Incus Data does not sell or otherwise distribute email addresses. We will not divulge your personal information to anyone unless specifically authorised by you.

If you need any further information, please contact us on tel: (27) 12-666-2020 or email

How can we help you?

Let us contact you about your training requirements. Just fill in a few details, and we’ll get right back to you.

Your Java tip is on its way!

Check that is an approved sender, so that your Java tips don’t land up in the spam folder.

Our privacy policy means your data is safe. You can unsubscribe from these tips at any time.