skip to content

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