How Java's + Operator Works for Strings
Fast recall
Quick revision
Core answer
+ is a language feature, not a guaranteed StringBuilder rewrite. Compile-time constant string expressions are folded and interned. For runtime concatenation, modern javac commonly emits invokedynamic and StringConcatFactory; the JVM chooses an efficient recipe. Repeatedly extending a growing String in a loop can still copy earlier text, so use one StringBuilder for that pattern.Whether you understand that + on Strings is not a primitive operation — the compiler transforms it, but only in a limited way that can produce O(n²) behavior in loops.
Assuming the compiler always uses one fixed StringBuilder translation, including inside loops. The compiler can only see single expressions — not loop iteration sequences.
Mention that since Java 9, the JVM uses a more dynamic concatenation strategy via invokedynamic (StringConcatFactory) which can be even more efficient than a classic StringBuilder chain — but the key interview point is still: use StringBuilder explicitly in loops.
Interview-ready guide
Interview answer
The + operator is Java's built-in string-concatenation syntax, but the language does not require one fixed implementation strategy. Constant expressions can be folded while compiling. For runtime values, current javac versions normally use invokedynamic concatenation machinery, while older bytecode explanations often show a generated StringBuilder chain.
For example, String label = "User " + id; is a clear one-line expression and should usually remain that way. By contrast, repeatedly doing result = result + item inside a loop creates a new immutable result at each step and repeatedly copies the growing content. A StringBuilder created before the loop, followed by append(item), expresses that one mutable buffer is being accumulated. So I do not promise an exact object count for every plus expression; I choose between readable one-off concatenation and an explicit builder for repeated construction.
How Java's + Operator Works for Strings
Direct Answer
String + is a language feature, not a guaranteed StringBuilder rewrite. Compile-time constant string expressions are folded and interned. For runtime concatenation, modern javac commonly emits invokedynamic and StringConcatFactory; the JVM chooses an efficient recipe. Repeatedly extending a growing String in a loop can still copy earlier text, so use one StringBuilder for that pattern.
Code Example
public class PlusOperatorDemo {
public static void main(String[] args) {
String name = "World";
// Single-line — compiler rewrites to StringBuilder chain, efficient
String greeting = "Hello, " + name + "!";
// Roughly becomes: new StringBuilder().append("Hello, ").append(name).append("!").toString()
System.out.println(greeting); // Hello, World!
// Loop — compiler CANNOT merge iterations, creates new object per iteration
String[] words = {"Java", " is", " powerful"};
String slow = "";
for (String w : words) {
slow = slow + w; // new StringBuilder + new String each time
}
// Correct approach — create StringBuilder once before the loop
StringBuilder sb = new StringBuilder();
for (String w : words) {
sb.append(w); // write to same buffer every time
}
String fast = sb.toString(); // one String at the very end
System.out.println(fast); // Java is powerful
}
}
Common Mistakes & Pitfalls
- Believing the compiler optimises
+inside loops — it only rewrites a single expression; it cannot merge iterations, soresult = result + wordin a loop is genuinely O(n²), not the efficientStringBuilderchain people assume. - Using
String +=to build large strings — every+=allocates a newStringBuilderand a newString; for thousands of items this shows up as real CPU and GC pressure. Use oneStringBuilder. - Calling
toString()inside the loop — even with a manualStringBuilder, materialising the string every iteration throws away the benefit; calltoString()exactly once after the loop. - Assuming
StringBufferis the answer for performance —StringBufferis synchronized and slower; in single-threaded code (the common case)StringBuilderis the right choice.
Check your understanding
Answer these without looking back at the article.
- What is the difference between StringBuilder and StringBuffer in Java?
- Why is String concatenation in a loop O(n²)?
- What is invokedynamic and how does Java 9 use it for string concatenation?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.