Concepts / String Data Type and Literals

String Data Type and Literals

The + operator performs concatenation when used with strings, joining them end to end to create a new string.

  • Programming

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

  • 5
  • 23
  • 2 3
Reveal answer

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.

characters firstcharacters next"Hello, "first string"Hello, world"new string"world"second string
What happens when two strings are joined end to end with the + operator?

Joining a greeting

Predict the result of "Hello, " + "world".

Start with the first string: The first operand contributes the characters Hello, including the space after the comma.

Append the second string: The second operand contributes world immediately after the first string.

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

firstnextfirstnext"Hello"no trailing space"Helloworld"joined directly"Hello, "space included"Hello, world"space preserved"world"second fragment"world"second fragment
How does the final text differ when the space is absent or included in a string fragment?

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.

+ " can"+ " learn""I"first fragment"I can"after adding " can""I can learn"after adding " learn"
How does each fragment contribute to the final left-to-right output?

Tracing three operands

Predict the result of "I" + " can" + " learn".

First combination: Start with "I" and append " can". The intermediate result is "I can".

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

ExpressionOperationResult
"2" + "3"String concatenation"23"
2 + 3Numeric addition5

Common Prediction Errors

  • Treating string concatenation as mathematical addition.

    The operands are strings, so + joins their characters instead of adding numeric values.

    Fix: Read the result as "23".

  • Expecting a space to appear between joined strings automatically.

    Concatenation places the second string immediately after the first, and no space was included.

    Fix: Include the space explicitly, as in "Hello, " + "world".

  • Ignoring the order of the operands.

    Concatenation preserves the characters from the operands in order.

    Fix: Trace the expression from left to right.

  • Stopping after combining only the first two strings in a chain.

    The + operator can be chained to concatenate more than two strings.

    Fix: Continue applying each + operation from left to right.

Practice the Trace

EASY

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.

First expression: "Good" is followed by the string " morning", which begins with a space, so the result is "Good morning".

Second expression: The quoted values are strings, so their characters are joined. The result is "45".

Third expression: The values are numbers, so + performs mathematical addition. The result is 9.

"Good morning", "45", and 9

Key Takeaways

  1. When + combines strings, it performs concatenation rather than mathematical addition.
  2. Concatenation places the characters of the operands end to end and produces a new string.
  3. Spaces and other desired characters must be included explicitly in the string operands.
  4. A chained expression can join more than two strings, with evaluation proceeding from left to right.
  5. Quoted string values such as "2" and "3" produce "23" when concatenated, while numeric values 2 and 3 produce 5 when added.

Key Takeaways

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