➗ Operators & Expressions · Beginner

String concatenation in Java

+ with a String converts operands; "1" + 2 + 3 vs 1 + 2 + "3".

🧩 The mystery"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"
🔮 Predict it

Your turn

What does this print?

System.out.println(2 + 3 + "x" + 2 + 3);
  1. 5x5
  2. 23x23
  3. 5x23
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

✗ Sum: 23
System.out.println("Sum: " + 2 + 3);

"Sum: " + 2 is already a String, so 3 is appended as text.

✓ Sum: 5
System.out.println("Sum: " + (2 + 3));

Parentheses force the numeric addition first.

🔮 Predict it

Your turn

'B' is 66. What does this print?

char c = 'B';
System.out.println(c + 1 + "?");
System.out.println("" + c + 1);
  1. 67? B1
  2. C? B1
  3. B1? 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.

⚠️ The trap

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 null

Concatenating 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 the real world

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. "1" + 2 + 3 → "123"
  2. 1 + 2 + "3" → "33"
  3. Use parentheses to add first: "Sum: " + (2 + 3)
  4. Building strings in a loop? Use StringBuilder
🤯 Did you know?

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");
  1. 6 6
  2. 15 33
  3. 123 33
  4. 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));
  1. Sum: 23 Sum: 5
  2. Sum: 5 Sum: 5
  3. Sum: 23 Sum: 23
  4. 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.

Next: 0.1 + 0.2 isn't 0.3 in Java, or in almost any language. Why can't computers add money?