String concatenation in Java
+ with a String converts operands; "1" + 2 + 3 vs 1 + 2 + "3".
"1" + 2 + 3 gives "123". 1 + 2 + "3" gives "33". Same three pieces, different answers. The + operator has a split personality.+ with a String joins
If either operand of + is a String, + concatenates (joins text). The other operand is converted to text with String.valueOf.
"Score: " + 42 // "Score: 42"
"Price: " + 9.5 // "Price: 9.5"
"ok? " + true // "ok? true"Left to right decides
+ is evaluated left to right. Numbers before the first String are added as numbers. Once a String appears, everything after it is joined as text.
1 + 2 + "3" // 3 + "3" → "33"
"1" + 2 + 3 // "12" + 3 → "123"Your turn
What does this print?
System.out.println(2 + 3 + "x" + 2 + 3);5x523x235x23
Show the answer
Left to right: 2 + 3 = 5 (numbers). 5 + "x" = "5x" (text from here on). Then "5x" + 2 + 3 = 5x23.
Adding inside a message
System.out.println("Sum: " + 2 + 3);"Sum: " + 2 is already a String, so 3 is appended as text.
System.out.println("Sum: " + (2 + 3));Parentheses force the numeric addition first.
Your turn
'B' is 66. What does this print?
char c = 'B';
System.out.println(c + 1 + "?");
System.out.println("" + c + 1);67? B1C? B1B1? B1
Show the answer
Line 1: c + 1 is numeric, 66 + 1 = 67, then "?" is appended: 67?. Line 2: "" + c is already a String, so 1 is appended as text: B1.
null joins as text
Concatenation turns null into the text "null", with no exception. It's how messages like "Welcome, null!" end up in front of real users.
String name = null;
System.out.println("Hi " + name);
// Hi nullConcatenating in a loop
Strings are immutable: they never change. So each report += line creates a brand-new String and copies everything built so far. In a big loop that's painfully slow. A StringBuilder appends in place.
var sb = new StringBuilder();
for (String line : lines) {
sb.append(line);
}
String report = sb.toString();In real projects
Building a 100,000-line export with += can turn seconds into minutes. StringBuilder is the professional default for loops; a single-line + is fine, because the compiler optimizes it.
Key takeaways
- "1" + 2 + 3 → "123"
- 1 + 2 + "3" → "33"
- Use parentheses to add first: "Sum: " + (2 + 3)
- Building strings in a loop? Use StringBuilder
Since Java 9, javac compiles String concatenation like a + b + c into a single invokedynamic call (JEP 280), letting the JVM choose the fastest joining strategy at runtime.
Practice questions
What does this print?
System.out.println("1" + 2 + 3);
System.out.println(1 + 2 + "3");- 6 6
- 15 33
- 123 33
- 123 123
Check your answer
123 33. + goes left to right. "1" + 2 is "12", then + 3 gives "123". In the second line 1 + 2 is numeric 3, then 3 + "3" gives "33".
What does this print?
System.out.println("Sum: " + 2 + 3);
System.out.println("Sum: " + (2 + 3));- Sum: 23 Sum: 5
- Sum: 5 Sum: 5
- Sum: 23 Sum: 23
- Sum: 5 Sum: 23
Check your answer
Sum: 23 Sum: 5. Without parentheses, "Sum: " + 2 is already a String, so 3 is appended. Parentheses force the numeric addition first.