Skip to content
Advertisement

How does the Java compiler handle StringBuilder(a + b + c + d)?

I am dealing with an application that contains lots of legacy code. What I see very often is string concatenation using “+” within a StringBuilder argument.
Example:

JavaScript

From what I’ve learned the compiler replaces a string concatenation that uses the + operator with StringBuilder().append().
I am afraid now the compiler will create a temporary StringBuilder to perform the concatenation then convert toString() and insert the result into the existing StringBuilder.

My Question is: Is the compiler able to optimize the nested StringBuilder away? And if not should I rewrite the code to save a few CPU cycles? It is obviously working but it hurts my eyes whenever I look at it.

Thanks for any insights!

Advertisement

Answer

The compiler optimizes concatenation of String literal constants:

JavaScript

is exactly equivalent, at runtime, to:

JavaScript

This also applies to any String compile-time constants, not just literals.

The concern you may have read about is when using non-constants:

JavaScript

is equivalent to:

JavaScript

and it would give you better performance if you re-wrote that as:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement