Type Conversion: Converting Strings to Numbers and Vice Versa
The + operator performs concatenation when used with strings, joining them end to end to create a new string.
Why the Same Symbol Behaves Differently
The + operator does not always represent mathematical addition. When it is used with strings, it performs concatenation: it joins the strings end to end and produces a new string. This means the result contains the characters from the operands in their original order rather than a calculated numerical total.
Reading a Concatenation Expression
To read a string concatenation expression, treat each string as a sequence of characters. The + operator places the next string immediately after the previous one. It does not rearrange, remove, or mathematically combine the characters. The final result is a new string containing all characters from the operands in order.
Joining Two Strings
Predict the result of "Good" + "Morning".
- 1First operand
The first string contributes the characters G, o, o, and d.
- 2Second operand
The second string is placed immediately after the first string.
- 3Join the characters
The characters form GoodMorning. No space appears because no space was included in either operand.
GoodMorning
Spaces and Other Characters
A space is a character. If a final string should contain a space or another separator, that character must be included explicitly as part of one of the strings being joined.
Adding a Space Explicitly
Compare "I" + "JK" with "I" + " " + "JK".
- 1Without a space string
The second operand begins immediately after the first, so the characters form IJK.
- 2With a space string
The separate string containing one space is placed between I and JK.
The first expression produces IJK. The second produces I JK.
Chaining Several Strings
The + operator can join more than two strings. For a chained expression, evaluation proceeds from left to right: the first two strings are joined, then the next string is joined to that result, and so on. The order of the operands determines the order of the characters in the final string.
What do you think happens?
What exact string results from "A" + "B" + "C"?
Reveal answer
ABC
The strings are joined from left to right. No space appears because no operand contains a space.
Conversion Before Using Plus
The key distinction is the kind of operation being performed. With strings, + performs concatenation and creates a new string. Numeric addition is a different operation. Therefore, when predicting an expression, first identify whether the operands are being treated as strings or numbers, then determine whether the operation joins characters or adds numerical values.
Mistakes Beginners Make
Common mistake Treating string concatenation as mathematical addition.
What it looks like"4" + "5" is expected to produce 9.Why it failsWhen the operands are strings, + joins their characters instead of calculating a numerical total.
Do this insteadRead the expression as a character-joining operation and predict the result as 45.
Common mistake Expecting a space to appear between joined strings.
What it looks like"Hello" + "World" is expected to produce Hello World.
Why it failsNo operand contains a space character.
Do this insteadInclude a separate space string when the result needs a space.
Common mistake Reversing the order of the operands.
What it looks like"left" + "right" is predicted as rightleft.Why it failsConcatenation preserves the operand order and proceeds from left to right.
Do this insteadCopy the first string first, then append each following string.
Common mistake Assuming that chaining changes the order of characters.
What it looks like"A" + "B" + "C" is predicted as ACB.Why it failsChained concatenation proceeds from left to right.
Do this insteadTrack each operand in sequence to obtain ABC.
Practice the Prediction
Predict the result of each expression before checking your reasoning: "Data" + "Base"; "Type" + " " + "Conversion"; "1" + "2" + "3". For each one, write the characters in order and note whether a space was explicitly included.
Hints
- Start with the complete first string.
- Append each later string from left to right.
- Look for a separate string that contains a space.
Checking the Practice Expressions
Determine the results of the three practice expressions.
- 1First expression
"Data" followed by "Base" produces DataBase.
- 2Second expression
The separate space string is placed between Type and Conversion, producing Type Conversion.
- 3Third expression
The three strings are joined from left to right, producing 123.
The results are DataBase, Type Conversion, and 123.
Key Takeaways
- 1With strings, the + operator performs concatenation rather than mathematical addition.
- 2Concatenation creates a new string containing all operand characters in their original order.
- 3Spaces and other required characters must be included explicitly.
- 4More than two strings can be chained, with evaluation proceeding from left to right.
- 5To predict the result, identify the operand type first and then decide whether + joins characters or performs numeric addition.
At a glance
- String concatenation joins strings end to end and produces a new string.
- The + operator preserves the order of characters supplied by its operands.
- A space appears only when a space character is explicitly included.
- Chained concatenation proceeds from left to right.
- String concatenation and numeric addition are different operations.