Java Reference
In-Depth Information
15.18. Additive Operators
The operators + and - are called the additive operators .
AdditiveExpression:
MultiplicativeExpression
AdditiveExpression + MultiplicativeExpression
AdditiveExpression - MultiplicativeExpression
The additive operators have the same precedence and are syntactically left-associative
(they group left-to-right).
If the type of either operand of a + operator is String , then the operation is string concatena-
tion.
Otherwise, the type of each of the operands of the + operator must be a type that is convert-
ible (§ 5.1.8 ) to a primitive numeric type, or a compile-time error occurs.
In every case, the type of each of the operands of the binary - operator must be a type that
is convertible (§ 5.1.8 ) to a primitive numeric type, or a compile-time error occurs.
15.18.1. String Concatenation Operator +
If only one operand expression is of type String , then string conversion (§ 5.1.11 ) is per-
formed on the other operand to produce a string at run time.
The result of string concatenation is a reference to a String object that is the concatenation
of the two operand strings. The characters of the left-hand operand precede the characters
of the right-hand operand in the newly created string.
The String object is newly created (§ 12.5 ) unless the expression is a compile-time constant
expression (§ 15.28 ) .
An implementation may choose to perform conversion and concatenation in one step
to avoid creating and then discarding an intermediate String object. To increase the
performance of repeated string concatenation, a Java compiler may use the StringBuffer
class or a similar technique to reduce the number of intermediate String objects that are
created by evaluation of an expression.
For primitive types, an implementation may also optimize away the creation of a
wrapper object by converting directly from a primitive type to a string.
Search WWH ::




Custom Search