Java Reference
In-Depth Information
Indeed, the test
x!=x
is
true
if and only if the value of
x
is NaN.
The methods
Float.isNaN
and
Double.isNaN
may also be used to test whether a
value is NaN.
• Positive zero and negative zero are considered equal.
For example,
-0.0==0.0
is
true
.
• Otherwise, two distinct floating-point values are considered unequal by the equal-
ity operators.
In particular, there is one value representing positive infinity and one value repres-
enting negative infinity; each compares equal only to itself, and each compares un-
equal to all other values.
Subject to these considerations for floating-point numbers, the following rules then hold
for integer operands or for floating-point operands other than NaN:
• The value produced by the
==
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
.
• The value produced by the
!=
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
.
15.21.2. Boolean Equality Operators
==
and
!=
If the operands of an equality operator are both of type
boolean
, or if one operand is of type
boolean
and the other is of type
Boolean
, then the operation is boolean equality.
The boolean equality operators are associative.
The result of
==
is
true
if the operands (after any required unboxing conversion) are both
true
or both
false
; otherwise, the result is
false
.
The result of
!=
is
false
if the operands are both
true
or both
false
; otherwise, the result is
true
.
15.21.3. Reference Equality Operators
==
and
!=
If the operands of an equality operator are both of either reference type or the null type,
then the operation is object equality.