Skip to main content
United StatesComputer ScienceSyllabus dot point

How are String objects created and joined, and what does string concatenation do with different operand types?

Topic 2.6 String Objects: Concatenation, Literals, and More: create String objects from literals or a constructor, concatenate strings with the + operator, and predict the result of mixing strings with numbers and escape sequences.

A focused answer to AP CSA Topic 2.6, covering String literals and the String constructor, concatenation with the plus operator, the rule that any value concatenated with a String becomes a String, left-to-right evaluation traps, and escape sequences, with a worked trace.

Generated by Claude Opus 4.89 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. Creating String objects
  3. Concatenation with +
  4. The left-to-right trap
  5. Escape sequences
  6. Try this

What this topic is asking

The College Board (Topic 2.6) wants you to create String objects (from a literal or the constructor), to concatenate strings with the + operator, and to predict the result when strings are mixed with numbers - including the key rule that any value joined to a String becomes part of the String. You should also recognize common escape sequences.

Creating String objects

String is a reference type, so a String variable holds a reference to the object, not the characters directly. Strings are also immutable: once created, the characters never change, so every String method that seems to modify a String actually returns a new String (Topic 2.7).

Concatenation with +

This makes building output easy:

String name = "Sam";
int age = 17;
System.out.println(name + " is " + age);   // Sam is 17

The left-to-right trap

Because + is evaluated left to right, the placement of a String changes everything:

  • "Score: " + 3 + 4 is "Score: 34". The first + (String + int) concatenates to "Score: 3", then + 4 concatenates again.
  • "Score: " + (3 + 4) is "Score: 7". The parentheses force the numeric addition first.
  • 3 + 4 + " points" is "7 points". Here 3 + 4 is numeric addition (both ints) before the String appears.

So whether + adds or joins depends on the operand types at each step, evaluated left to right.

Escape sequences

Inside a String literal, a backslash starts an escape sequence representing a character you cannot type directly:

  • \" - a double quote inside the String.
  • \\ - a single backslash.
  • \n - a newline (moves to a new line).

For example, "She said \"hi\"\nbye" prints She said "hi" on one line and bye on the next.

Try this

Q1. State the value of the String produced by "" + 2 + 3. [1 point]

  • Cue. "23". The empty String makes the first + a concatenation, so 2 and 3 are joined as text, not added.

Q2. Write an expression that produces the String "Sum is 5" from the numbers 2 and 3. [2 points]

  • Cue. "Sum is " + (2 + 3). The parentheses force 2 + 3 to add to 5 before it is concatenated.

Exam-style practice questions

Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

AP 2022 (style)1 marksMultiple choice. What is printed by the following statement? ```java System.out.println("Sum: " + 3 + 4); ``` (A) `Sum: 7` (B) `Sum: 34` (C) `Sum: 12` (D) `7` (E) `34`
Show worked answer →

The answer is (B).

The + operators are evaluated left to right. "Sum: " + 3 concatenates because the left operand is a String, producing "Sum: 3". Then "Sum: 3" + 4 concatenates again, producing "Sum: 34". The 3 and 4 are never added as numbers, because once a String is involved, + means concatenation. To get "Sum: 7" you would write "Sum: " + (3 + 4), forcing the addition first. (A) assumes numeric addition; (C), (D) and (E) miscompute.

Markers reward applying left-to-right evaluation and recognizing that String concatenation turns the numbers into text.

AP 2019 (style)3 marksFree response (code writing). Two `int` variables `hours` and `minutes` are given. Write a code segment that builds a String `time` in the form `H:MM is the time` (for example, if `hours` is 9 and `minutes` is 5, the relevant values are joined with concatenation), and prints it. You do not need to pad the minutes; just concatenate the values with the literal text and a colon between hours and minutes.
Show worked answer →

A 3-point question testing concatenation of variables and literals.

String time = hours + ":" + minutes + " is the time";
System.out.println(time);

Point 1: concatenating hours, the ":" literal and minutes so the numbers become part of the String. Point 2: appending the literal " is the time". Point 3: printing the result. Because the first operand hours is an int and the next operand ":" is a String, the whole expression is built by left-to-right concatenation; the colon literal ensures the numbers are not added.

Related dot points

Sources & how we know this