The type of the operand expression of the unary -
operator must be a primitive
numeric type, or a compile-time error occurs. Unary numeric promotion (§5.6.1)
is performed on the operand. The type of the unary minus expression is the promoted type of the operand.
At run time, the value of the unary plus expression is the arithmetic negation of the promoted value of the operand.
For integer values, negation is the same as subtraction from zero. Java uses two's-complement representation for integers, and the range of two's-complement 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
equals +0.0
, but -x
equals -0.0
. Unary minus merely inverts the sign of a floating-point number. Special cases of interest: