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.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
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 + 4is"Score: 34". The first+(String + int) concatenates to"Score: 3", then+ 4concatenates again."Score: " + (3 + 4)is"Score: 7". The parentheses force the numeric addition first.3 + 4 + " points"is"7 points". Here3 + 4is 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, so2and3are 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 force2 + 3to add to5before 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
- Topic 2.7 String Methods: call the String methods in the AP Java subset (length, substring, indexOf, equals, compareTo), respecting zero-based indexing and the immutability of String objects.
A focused answer to AP CSA Topic 2.7, covering the required String methods length, substring (both forms), indexOf, equals and compareTo, zero-based indexing, the half-open range of substring, why == differs from equals, and String immutability, with a worked trace.
- Topic 2.5 Calling a Non-void Method: call a method that returns a value, and use the returned value by storing it, printing it, or including it in an expression.
A focused answer to AP CSA Topic 2.5, covering methods that return a value, the return type, using a returned value in an assignment, expression or print, the difference from a void method, and chaining method calls, with a worked trace.
- Topic 2.2 Creating and Storing Objects (Instantiation): use the new keyword to call a constructor and instantiate an object, choosing the correct constructor by its parameters, and store the result in a reference variable.
A focused answer to AP CSA Topic 2.2, covering the new keyword, constructors and how they initialise an object, choosing a constructor by its parameter list, matching argument types, storing the reference, and the meaning of null, with a worked example.
- Topic 1.2 Variables and Data Types: identify the primitive types int, double and boolean, and declare, initialise and use variables of those types.
A focused answer to AP CSA Topic 1.2, covering the difference between primitive and reference types, the three exam primitives int, double and boolean, declaring and initialising variables, valid identifiers, and constants, with a traced worked example.
- Topic 2.1 Objects: Instances of Classes: explain the relationship between a class and its objects, and describe an object as an instance of a class with state and behavior.
A focused answer to AP CSA Topic 2.1, covering the class-object relationship, what it means for an object to be an instance, the difference between attributes (state) and methods (behavior), and reference versus primitive variables, with a worked example.
Sources & how we know this
- AP Computer Science A Course and Exam Description — College Board (2025)