If the operands of an equality operator are both of primitive numeric type, binary
numeric promotion is performed on the operands (§5.6.2). If the promoted type of
the operands is int
or long
, then an integer equality test is performed; if the promoted type is float
or double
, then a floating-point equality test is performed.
Floating-point equality testing is performed in accordance with the rules of the IEEE 754 standard:
==
is false
but the result of !=
is true
. Indeed, the test x!=x
is true if and only if the value of x
is NaN. (The methods Float.isNaN
(§20.9.19) and Double.isNaN
(§20.10.17) may also be used to test whether a value is NaN.)
-0.0==0.0
is true
, for example.
Subject to these considerations for floating-point numbers, the following rules then hold for integer operands or for floating-point operands other than NaN:
==
operator is true
if the value of the left-hand operand is equal to the value of the right-hand operand; otherwise, the result is false
.
!=
operator is true
if the value of the left-hand operand is not equal to the value of the right-hand operand; otherwise, the result is false
.