String Data Type and Literals
The + operator performs concatenation when used with strings, joining them end to end to create a new string.
A Different Meaning for +
The + operator does not always mean mathematical addition. When its operands are strings, it performs concatenation: it joins the strings end to end and produces a new string. The important question is therefore not only which operator appears, but also what kind of values it is combining.
What do you think happens?
What result should you predict for the expression "2" + "3"?
Reveal answer
23
The operands are strings, so + joins their characters in order. It does not perform mathematical addition.
Joining Fragments
Concatenation treats each string as a sequence of characters. The first operand contributes its characters, and the next operand is placed immediately after them. The result is one new string containing all characters from the operands in their original order.
Joining a greeting
Predict the result of "Hello, " + "world".
- 1Start with the first string
The first operand contributes the characters Hello, including the space after the comma.
- 2Append the second string
The second operand contributes world immediately after the first string.
- 3Read the combined result
Reading from left to right gives Hello, world.
"Hello, world"
Spaces Are Characters
Concatenation does not insert a separator for you. If the final result should contain a space, that space must be included explicitly as part of one of the strings. Compare joining "Hello" and "world" with joining "Hello, " and "world": only the second pair contains a space between the words.
Reading Left to Right
You can chain the + operator to concatenate more than two strings. Evaluation proceeds from left to right, so each new string is placed after the result built so far.
Tracing three operands
Predict the result of "I" + " can" + " learn".
- 1First combination
Start with "I" and append " can". The intermediate result is "I can".
- 2Second combination
Append " learn" to the intermediate result. The final result is "I can learn".
"I can learn"
Strings and Numbers
The same symbol can produce different kinds of results depending on the operands. With string operands such as "2" and "3", + concatenates and produces "23". With numeric operands such as 2 and 3, + performs mathematical addition and produces 5. The quotation marks in the first expression show the values being used as string fragments in this comparison.
| Expression | Operation | Result |
|---|---|---|
| "2" + "3" | String concatenation | "23" |
| 2 + 3 | Numeric addition | 5 |
The operands determine whether + joins string characters or adds numeric values.
Common Prediction Errors
Common mistake Treating string concatenation as mathematical addition.
What it looks like"2" + "3" is predicted as 5.Why it failsThe operands are strings, so + joins their characters instead of adding numeric values.
Do this insteadRead the result as "23".Common mistake Expecting a space to appear between joined strings automatically.
What it looks like"Hello" + "world" is expected to produce "Hello world".Why it failsConcatenation places the second string immediately after the first, and no space was included.
Do this insteadInclude the space explicitly, as in "Hello, " + "world".
Common mistake Ignoring the order of the operands.
What it looks like"world" + "Hello" is expected to produce the same result as "Hello" + "world".
Why it failsConcatenation preserves the characters from the operands in order.
Do this insteadTrace the expression from left to right.
Common mistake Stopping after combining only the first two strings in a chain.
What it looks like"I" + " can" + " learn" is read as only "I can".Why it failsThe + operator can be chained to concatenate more than two strings.
Do this insteadContinue applying each + operation from left to right.
Practice the Trace
Predict the result of each expression before checking your reasoning: "Good" + " morning"; "4" + "5"; and 4 + 5. For the first expression, identify whether the space is included. For the second and third expressions, classify the operands before deciding what + does.
Hints
- Trace each expression from left to right.
- A space appears only if it is included in a string operand.
- Compare the quoted operands with the unquoted numeric operands.
Checking the practice expressions
Determine the results of "Good" + " morning", "4" + "5", and 4 + 5.
- 1First expression
"Good" is followed by the string " morning", which begins with a space, so the result is "Good morning".
- 2Second expression
The quoted values are strings, so their characters are joined. The result is "45".
- 3Third expression
The values are numbers, so + performs mathematical addition. The result is 9.
"Good morning", "45", and 9
Key Takeaways
- 1When + combines strings, it performs concatenation rather than mathematical addition.
- 2Concatenation places the characters of the operands end to end and produces a new string.
- 3Spaces and other desired characters must be included explicitly in the string operands.
- 4A chained expression can join more than two strings, with evaluation proceeding from left to right.
- 5Quoted string values such as "2" and "3" produce "23" when concatenated, while numeric values 2 and 3 produce 5 when added.
At a glance
- The + operator concatenates strings by joining them end to end.
- String concatenation preserves the characters and order of its operands.
- A space must be included explicitly if it should appear in the result.
- Chained concatenation proceeds from left to right.
- String concatenation and numeric addition produce different results because the operands have different kinds of values.