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.

"2"string2number+concatenation+numeric addition"3"string3number"23"new string5numeric resultjoinedadded
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.

Worked example

Joining Two Strings

Predict the result of "Good" + "Morning".

  1. 1
    First operand

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

  2. 2
    Second operand

    The second string is placed immediately after the first string.

  3. 3
    Join the characters

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

Result

GoodMorning

"Good"first string"GoodMorning"one new string"Morning"second stringfirstthen
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

Definition

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.

Worked example

Adding a Space Explicitly

Compare "I" + "JK" with "I" + " " + "JK".

  1. 1
    Without a space string

    The second operand begins immediately after the first, so the characters form IJK.

  2. 2
    With a space string

    The separate string containing one space is placed between I and JK.

Result

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.

"A"first operand"B"second operand"C"third operand"ABC"final stringthenthenproduces
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.

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

Mistakes Beginners Make

  • Common mistake

    Treating string concatenation as mathematical addition.

    What it looks like
    "4" + "5" is expected to produce 9.
    Why it fails

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

    Do this instead

    Read 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 fails

    No operand contains a space character.

    Do this instead

    Include 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 fails

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

    Do this instead

    Copy 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 fails

    Chained concatenation proceeds from left to right.

    Do this instead

    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.
Worked example

Checking the Practice Expressions

Determine the results of the three practice expressions.

  1. 1
    First expression

    "Data" followed by "Base" produces DataBase.

  2. 2
    Second expression

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

  3. 3
    Third expression

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

Result

The results are DataBase, Type Conversion, and 123.

Key Takeaways

  1. 1
    With strings, the + operator performs concatenation rather than mathematical addition.
  2. 2
    Concatenation creates a new string containing all operand characters in their original order.
  3. 3
    Spaces and other required characters must be included explicitly.
  4. 4
    More than two strings can be chained, with evaluation proceeding from left to right.
  5. 5
    To 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.

Related concepts

More concepts starting with T