Java Reference
In-Depth Information
EqualityExpression:
RelationalExpression
EqualityExpression
==
RelationalExpression
EqualityExpression
!=
RelationalExpression
The equality operators are syntactically left-associative (they group left-to-right).
However, this fact is essentially never useful. For example,
a==b==c
parses as
(a==b)==c
. The result type of
a==b
is always
boolean
, and
c
must therefore be of type
boolean
or a compile-time error occurs. Thus,
a==b==c
does not test to see whether
a
,
b
,
and
c
are all equal.
The equality operators are commutative if the operand expressions have no side effects.
The equality operators are analogous to the relational operators except for their lower pre-
cedence. Thus,
a<b==c<d
is
true
whenever
a<b
and
c<d
have the same truth value.
to numeric type, or two operands of type
boolean
or
Boolean
, or two operands that are each
of either reference type or the null type. All other cases result in a compile-time error.
The type of an equality expression is always
boolean
.
In all cases,
a!=b
produces the same result as
!(a==b)
.
15.21.1. Numerical Equality Operators
==
and
!=
If the operands of an equality operator are both of numeric type, or one is of numeric type
formed on the operands (§
5.6.2
).
Note that binary numeric promotion performs value set conversion (§
5.1.13
) and may
perform unboxing conversion (§
5.1.8
).
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.
Comparison is carried out accurately on floating-point values, no matter what value sets
their representing values were drawn from.
Floating-point equality testing is performed in accordance with the rules of the IEEE 754
standard:
• If either operand is NaN, then the result of
==
is
false
but the result of
!=
is
true
.