Java Reference
In-Depth Information
15.15.4. Unary Minus Operator
-
The type of the operand expression of the unary
-
operator must be a type that is convertible
The type of the unary minus expression is the promoted type of the operand.
Note that unary numeric promotion performs value set conversion (§
5.1.13
). Whatever
value set the promoted operand value is drawn from, the unary negation operation is carried
out and the result is drawn from that same value set. That result is then subject to further
value set conversion.
At run time, the value of the unary minus expression is the arithmetic negation of the pro-
moted value of the operand.
For integer values, negation is the same as subtraction from zero. The Java programming
language uses two's-complement representation for integers, and the range of two's-com-
plement values is not symmetric, so negation of the maximum negative
int
or
long
results
in that same maximum negative number. Overflow occurs in this case, but no exception is
thrown. For all integer values
x
,
-x
equals
(~x)+1
.
For floating-point values, negation is
not
the same as subtraction from zero, because if
x
is
+0.0
, then
0.0-x
is
+0.0
, but
-x
is
-0.0
. Unary minus merely inverts the sign of a floating-point
number. Special cases of interest:
• If the operand is an infinity, the result is the infinity of opposite sign.
• If the operand is a zero, the result is the zero of opposite sign.
15.15.5. Bitwise Complement Operator
~
The type of the operand expression of the unary
~
operator must be a type that is convertible
Unary numeric promotion (§
5.6.1
) is performed on the operand. The type of the unary bit-
wise complement expression is the promoted type of the operand.
At run time, the value of the unary bitwise complement expression is the bitwise comple-
ment of the promoted value of the operand. In all cases,
~x
equals
(-x)-1
.