Concepts / Type Conversion: Converting Strings to Numbers and Vice Versa

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.

  • Programming

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.

joinedadded"2"string2number+concatenation+numeric addition"3"string3number"23"new string5numeric result
How does the same + operator produce different results when its operands are strings versus numbers?

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".

First operand: The first string contributes the characters G, o, o, and d.

Second operand: The second string is placed immediately after the first string.

Join the characters: The characters form GoodMorning. No space appears because no space was included in either operand.

GoodMorning

firstthen"Good"first string"GoodMorning"one new string"Morning"second string
What happens to the order and contents of two or more strings as the + operator joins them into one new string?

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".

Without a space string: The second operand begins immediately after the first, so the characters form IJK.

With 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"?

  • ABC
  • CBA
  • A B C
Reveal answer

Answer: ABC

The strings are joined from left to right. No space appears because no operand contains a space.

thenthenproduces"A"first operand"B"second operand"C"third operand"ABC"final string
How does each operand move into the final output string, and what exact sequence of characters appears?

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.

used as stringconvertedbecomesused as number"12"string valueconcatenationstring operationtype conversionchanges operand typeadditionnumeric operation12number value
How does changing an operand from a string representation to a number affect the operation performed by +?

Mistakes Beginners Make

  • Treating string concatenation as mathematical addition.

    When the operands are strings, + joins their characters instead of calculating a numerical total.

    Fix: Read the expression as a character-joining operation and predict the result as 45.

  • Expecting a space to appear between joined strings.

    No operand contains a space character.

    Fix: Include a separate space string when the result needs a space.

  • Reversing the order of the operands.

    Concatenation preserves the operand order and proceeds from left to right.

    Fix: Copy the first string first, then append each following string.

  • Assuming that chaining changes the order of characters.

    Chained concatenation proceeds from left to right.

    Fix: Track each operand in sequence to obtain ABC.

Practice the Prediction

EASY

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.

First expression: "Data" followed by "Base" produces DataBase.

Second expression: The separate space string is placed between Type and Conversion, producing Type Conversion.

Third expression: The three strings are joined from left to right, producing 123.

The results are DataBase, Type Conversion, and 123.

Key Takeaways

  1. With strings, the + operator performs concatenation rather than mathematical addition.
  2. Concatenation creates a new string containing all operand characters in their original order.
  3. Spaces and other required characters must be included explicitly.
  4. More than two strings can be chained, with evaluation proceeding from left to right.
  5. To predict the result, identify the operand type first and then decide whether + joins characters or performs numeric addition.

Key Takeaways

  • 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.