Java Reference
In-Depth Information
15.18.2. Additive Operators (
+
and
-
) for Numeric Types
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 oper-
ands.
Note that binary numeric promotion performs value set conversion (§
5.1.13
) and may
perform unboxing conversion (§
5.1.8
).
The type of an additive expression on numeric operands is the promoted type of its oper-
ands.
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.
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 oc-
curs, 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 754
arithmetic:
• If either operand is NaN, the result is NaN.
• The sum of two infinities of opposite sign is NaN.
• The sum of two infinities of the same sign is the infinity of that sign.
• The sum of an infinity and a finite value is equal to the infinite operand.
• The sum of two zeros of opposite sign is positive zero.
• The sum of two zeros of the same sign is the zero of that sign.
• The sum of a zero and a nonzero finite value is equal to the nonzero operand.