Java Quickie: Speeding Up String Concatenation

Speeding up string concatenation in Java is easy and simple. Just follow along the steps in this 5 minute tutorial and you’re good.

Speeding up string concatenation in Java is explained with little words. Just don’t. Don’t concate strings, use the StringBuilder class! Why? Because concating strings in Java will create multiple String objects. For example

Will put "Hello" and " World!" individually into the string pool. And put the concatenated String "Hello World!" as new string object in the Heap. If you are concating much more strings, the performance will decrease.

StringBuilder on the other hand behaves as an array of characters. Meaning it’s more performance friendly because the objects are mutable.

Let’s replicate above example with StringBuilder:

str.toString() gives you the string you just built. (Run this example)