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-
In every case, the type of each of the operands of the binary
-
operator must be a type that
15.18.1. String Concatenation Operator
+
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.
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.