skip to content

String Concatenation

The compiler folds literal concatenation at compile time and translates runtime + into an invokedynamic call to StringConcatFactory — but it cannot fuse concatenation across loop iterations. That is why + in a loop is the classic performance answer.

part ofJavaoverview, primer and where to startread it →
on this pageshow

questions

5

How does the + operator work for joining strings in Java, and what does it produce?

level: juniorimportance: must knowfreq 80%

answer

  1. with any String operand = concatenation, returns NEW String
  2. immutable: originals never change
  3. non-String converted: toString(), null -> "null"
  4. left-to-right: 1+2+"x"="3x", "x"+1+2="x12"

basics

~10 s

The + operator joins two strings into a new string. Because String objects can't be changed, every + makes a brand-new String holding the combined text; the originals stay untouched.

solid answer

~40 s

In Java the + operator, when at least one operand is a String, performs concatenation: it produces a new String whose characters are the left operand followed by the right operand. Strings are immutable, so the originals are never modified; a fresh String is created for the result. Non-String operands are first converted to their string form (numbers become their digits, objects via toString(), null becomes the literal "null"). Evaluation is left-to-right, so "a" + 1 + 2 yields "a12" while 1 + 2 + "a" yields "3a" because the leading 1 + 2 is integer addition before any string is involved. The operator is convenient and readable for a small, fixed number of pieces.

go deeper

for a junior

Knows + joins strings and that the result is a new string, and can predict simple outputs.

for a middle

Explains immutability and the left-to-right / mixed-type conversion rules including null handling.

for a senior

Connects the readability of + for fixed pieces to when to prefer alternatives, and reasons about operand conversion costs.

for a principal

Frames concatenation choices as API/readability guidance for a team and knows the compiler erases most micro-cost for fixed expressions.

## What concatenation means Concatenation is gluing two pieces of text end to end. `"foo" + "bar"` gives `"foobar"`. In Java the same `+` symbol means two different things depending on the operands: if **both** sides are numbers it is arithmetic addition; if **at least one** side is a `String` it is string concatenation. ## Strings are immutable A Java `String` is **immutable** — once created, its characters can never change. So `+` cannot "append" to an existing string. Instead it builds and returns a **new** `String` object containing the combined characters. The inputs are left exactly as they were. This is why `a + b` does not change `a` or `b`. ## How non-strings are converted If an operand is not a `String`, Java converts it to text first: - Primitives (`int`, `double`, `boolean`, `char`...) use their textual form, e.g. `42` → `"42"`, `true` → `"true"`. - Objects call their `toString()` method, e.g. a `LocalDate` becomes `"2026-06-20"`. - A `null` reference becomes the four-character literal `"null"` (it does NOT throw a NullPointerException). ## Left-to-right evaluation — the classic trap `+` groups left to right and has the same precedence whether it is addition or concatenation. So expressions are evaluated one step at a time: - `"x" + 1 + 2` → `("x" + 1) + 2` → `"x1" + 2` → `"x12"`. - `1 + 2 + "x"` → `(1 + 2) + "x"` → `3 + "x"` → `"3x"`. Until a `String` enters the chain, `+` stays arithmetic. Wrap arithmetic you want preserved in parentheses: `"sum=" + (1 + 2)` → `"sum=3"`. ## When to use it For a small, fixed handful of parts, `+` is the clearest, most readable choice and the compiler optimizes it well. The pitfalls (covered in other questions) appear mainly with loops and very large numbers of pieces.

  • What does "" + null produce, and does it throw?
    It produces the three-letter... four-character string "null" and does not throw; null operands are converted to the literal "null".
  • Why does 1 + 2 + "x" differ from "x" + 1 + 2?
    Left-to-right evaluation: in the first, 1+2 is integer addition (3) before the String joins, giving "3x"; in the second the String comes first so each number is concatenated, giving "x12".

saying these in an interview costs you the question

  • Thinking + modifies the original string in place
  • Believing concatenating null throws NPE (it yields "null")
  • Assuming "x" + 1 + 2 equals "x3" (it is "x12")

context

open as a page

How does the Java compiler translate the + string concatenation operator, and how has that changed in modern Java?

level: middleimportance: must knowfreq 68%

basics

~20 s

The + you write is not a real method on String. The compiler turns it into bytecode that actually builds the joined string. Older Java used a StringBuilder behind the scenes; modern Java uses a special hook called invokedynamic.

open as a page

Why is using + to build a string inside a loop a performance pitfall, and what should you use instead?

level: seniorimportance: must knowfreq 72%

basics

~20 s

Each + in a loop makes a whole new string and copies all the characters so far. Over many iterations that copying gets very expensive. Use a StringBuilder and append in the loop, then call toString() once at the end.

open as a page

What is compile-time constant folding of string concatenation, and when does it apply?

level: middleimportance: should knowfreq 45%

basics

~20 s

If you join strings that are all known at compile time (literals or final constants), the compiler joins them itself and stores the single finished string. No work happens at runtime, and the result is a pooled, interned string literal.

open as a page

How would you decide between +, StringBuilder, and String.join/StringJoiner/Collectors.joining for a given concatenation task?

level: seniorimportance: should knowfreq 40%

basics

~10 s

Use + for a few fixed pieces in one statement. Use StringBuilder when building up text in a loop. Use String.join or Collectors.joining when gluing a collection together with a separator.

open as a page