The Binary String Concatenation Operator + – Basic Elements, Primitive Data Types, and Operators

2.9 The Binary String Concatenation Operator +

The binary operator + is overloaded in the sense that the operation performed is determined by the type of the operands. When one of the operands is a String object, a string concatenation is performed rather than numeric addition. String concatenation results in a newly created String object in which the characters in the text representation of the left-hand operand precede the characters in the text representation of the right-hand operand. It might be necessary to perform a string conversion on the non-String operand before the string concatenation can be performed. The String class is discussed in §8.4, p. 439.

A string conversion is performed on the non-String operand as follows:

  • For an operand of a primitive data type, its value is converted to a text representation.
  • For all reference value operands, a text representation is constructed by calling the no-argument toString() method on the referred object. Most classes override this method from the Object class so as to provide a more meaningful text representation of their objects. Discussion of the toString() method can be found in §8.2, p. 425.
  • Values like true, false, and null have text representations that correspond to their names. A reference variable with the value null also has the text representation “null” in this context.

The operator + is left associative and has the same precedence level as the additive operators, whether it is performed as a string concatenation or as a numeric addition.

Click here to view code image

String strVal = “” + 2020;                    // (1) “2020”
String theName = ” Uranium”;
theName = ” Pure” + theName;                  // (2) ” Pure Uranium”
String trademark1 = 100 + “%” + theName;      // (3) “100% Pure Uranium”

Since the + operator is left-associative, the evaluation at (3) proceeds as follows: The int value 100 is concatenated with the string literal “%”, followed by concatenation with the contents of the String object referred to by the theName reference.

Note that using the character literal ‘%’ instead of the string literal “%” in line (2) does not give the same result:

Click here to view code image

String trademark2 = 100 + ‘%’ + theName;      // (4) “137 Pure Uranium”

Integer addition is performed by the first + operator: 100 + ‘%’; that is, (100 + 37).

Caution should be exercised because the + operator might not be applied as intended, as shown by the following example:

Click here to view code image

System.out.println(“We can put two and two together and get ” + 2 + 2);   // (5)

This statement prints “We can put two and two together and get 22”. String concatenation proceeds from left to right: The String literal is concatenated with the first int literal 2, followed by concatenation with the second int literal 2. Both occurrences of the + operator are treated as string concatenation. To convey the intended meaning of the sentence, parentheses are necessary:

Click here to view code image

System.out.println(“We can put two and two together and get ” + (2 + 2)); // (6)

This statement prints “We can put two and two together and get 4”, since the parentheses enforce integer addition in the expression (2 + 2) before string concatenation is performed with the contents of the String operand.

The following statement will print the correct result, even without the parentheses, because the * operator has higher precedence than the + operator:

Click here to view code image

System.out.println(“2 * 2 = ” + 2 * 2);       // (7) 2 * 2 = 4

Creation of temporary String objects might be necessary to store the results of performing successive string concatenations in a String-valued expression. For a String-valued constant expression ((1), (5), (6), and (7) in the preceding examples), the compiler computes such an expression at compile time, and the result is treated as a string literal in the program. The compiler uses a string builder to avoid the overhead of temporary String objects when applying the string concatenation operator (+) in String-valued non-constant expressions ((2), (3), and (4) in the preceding examples), as explained in §8.5, p. 469.