| CHAPTER 15: Expressions |
Previous |
Java Language |
Index |
Next |
The operators + and - are called the additive operators. They have the same precedence and are syntactically left-associative (they group left-to-right).
AdditiveExpression: MultiplicativeExpression AdditiveExpression + MultiplicativeExpression AdditiveExpression - MultiplicativeExpression
If the type of either operand of a + operator is String , then the operation is string concatenation.
Otherwise, the type of each of the operands of the + operator must be 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 primitive numeric type, or a compile-time error occurs.
If only one operand expression is of type String
, then string conversion is performed on the other operand to produce a string at run time. The result is a reference to a newly created 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.
Any type may be converted to type String by string conversion.
A value x of primitive type T is first converted to a reference value as if by giving it as an argument to an appropriate class instance creation expression:
This reference value is then converted to type String by string conversion.
Now only reference values need to be considered. If the reference is null
, it is converted to the string "null
" (four ASCII characters n
, u
, l
, l
). Otherwise, the conversion is performed as if by an invocation of the toString
method of the referenced object with no arguments; but if the result of invoking the toString
method is null
, then the string "null
" is used instead. The toString
method (S20.1.2) is defined by the primordial class Object
(S20.1); many classes override it, notably Boolean
, Character
, Integer
, Long
, Float
, Double,
and 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 (S20.13) or a similar technique to reduce the number of intermediate String objects that are created by evaluation of an expression.
For primitive objects, an implementation may also optimize away the creation of a wrapper object by converting directly from a primitive type to a string.
The example expression:
"The square root of 2 is " + Math.sqrt(2)
produces the result:
"The square root of 2 is 1.4142135623730952"
The + operator is syntactically left-associative, no matter whether it is later determined by type analysis to represent string concatenation or addition. In some cases care is required to get the desired result. For example, the expression:
a + b + c
is always regarded as meaning:
(a + b) + c
Therefore the result of the expression:
1 + 2 + " fiddlers"
is:
"3 fiddlers"
but the result of:
"fiddlers " + 1 + 2
is:
"fiddlers 12"
In this jocular little example:
class Bottles {
static void printSong(Object stuff, int n) {
String plural = "s";
loop: while (true) {
System.out.println(n + " bottle" + plural
+ " of " + stuff + " on the wall,");
System.out.println(n + " bottle" + plural
+ " of " + stuff + ";");
System.out.println("You take one down "
+ "and pass it around:");
--n;
plural = (n == 1) ? "" : "s";
if (n == 0)
break loop;
System.out.println(n + " bottle" + plural
+ " of " + stuff + " on the wall!");
System.out.println();
}
System.out.println("No bottles of " +
stuff + " on the wall!");
}
}
the method printSong will print a version of a children's song. Popular values for stuff include "pop" and "beer" ; the most popular value for n is 100 . Here is the output that results from Bottles.printSong("slime", 3) :
3 bottles of slime on the wall, 3 bottles of slime; You take one down and pass it around: 2 bottles of slime on the wall! 2 bottles of slime on the wall, 2 bottles of slime; You take one down and pass it around: 1 bottle of slime on the wall! 1 bottle of slime on the wall, 1 bottle of slime; You take one down and pass it around: No bottles of slime on the wall!
In the code, note the careful conditional generation of the singular "bottle " when appropriate rather than the plural "bottles "; note also how the string concatenation operator was used to break the long constant string:
"You take one down and pass it around:"
into two pieces to avoid an inconveniently long line in the source code.
The binary + operator performs addition when applied to two operands of numeric type, producing the sum of the operands. The binary - operator performs subtraction, producing the difference of two numeric operands.
Binary numeric promotion is performed on the operands (S5.6.2). The type of an additive expression on numeric operands is the promoted type of its operands. If this promoted type is int or long , then integer arithmetic is performed; if this promoted type is float or double , then floating-point arithmetic is performed.
Addition is a commutative operation if the operand expressions have no side effects. Integer addition is associative when the operands are all of the same type, but floating-point addition is not associative.
If an integer addition overflows, then the result is the low-order bits of the mathematical sum as represented in some sufficiently large two's-complement format. If overflow occurs, then the sign of the result is not the same as the sign of the mathematical sum of the two operand values.
The result of a floating-point addition is determined using the following rules of IEEE arithmetic:
The binary - operator performs subtraction when applied to two operands of numeric type producing the difference of its operands; the left-hand operand is the minuend and the right-hand operand is the subtrahend. For both integer and floating-point subtraction, it is always the case that a-b produces the same result as a+(-b) . Note that, for integer values, subtraction from zero is the same as negation. However, for floating-point operands, subtraction from zero is not the same as negation, because if x is +0.0 , then 0.0-x equals +0.0 , but -x equals -0.0 .
Despite the fact that overflow, underflow, or loss of information may occur, evaluation of a numeric additive operator never throws a run-time exception.
| © 1996 Sun Microsystems, Inc. All rights reserved. |