15.21 Equality Operators
The equality operators are syntactically left-associative (they group
left-to-right), but 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.
EqualityExpression:
RelationalExpression
EqualityExpression == RelationalExpression
EqualityExpression != RelationalExpression The == (equal to) and the!= (not equal to) operators are analogous to the relational
operators except for their lower precedence. Thus, a
In all cases, a!=b produces the same result as !(a==b). The equality
operators are commutative if the operand expressions have no side
effects.
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 and the other is convertible (§5.1.8) to
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. Note that
binary numeric promotion performs value set conversion (§5.1.13) and
unboxing conversion (§5.1.8). 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. 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. Therefore, -0.0==0.0 is true, for example.
Otherwise, two distinct floating-point values are considered unequal
by the equality operators. In particular, there is one value
representing positive infinity and one value representing negative
infinity; each compares equal only to itself, and each compares
unequal 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. If one of the operands is of type Boolean it is
subjected to unboxing conversion (§5.1.8).
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. Thus != behaves the same as ^
(§15.22.2) when applied to boolean operands.
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. A
compile-time error occurs if it is impossible to convert the type of
either operand to the type of the other by a casting conversion
(§5.5). The run-time values of the two operands would necessarily be
unequal.
At run time, the result of == is true if the operand values are both
null or both refer to the same object or array; otherwise, the result
is false.
The result of != is false if the operand values are both null or both
refer to the same object or array; otherwise, the result is true.
While == may be used to compare references of type String, such an
equality test determines whether or not the two operands refer to the
same String object. The result is false if the operands are distinct
String objects, even if they contain the same sequence of characters.
The contents of two strings s and t can be tested for equality by the
method invocation s.equals(t). See also §3.10.5.