Equality Operators

15.21 Equality Operators

The operators == (equal to) and != (not equal to) are called the equality operators.

EXPRESSIONS Numerical Equality Operators == and != 15.21.1

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 precedence. Thus, a<b==c<d is true whenever a<b and c<d have the same truth value.

The equality operators may be used to compare two operands that are convertible (§5.1.8) 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 and the other is convertible (§5.1.8) to numeric type, binary numeric promotion is performed 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.

15.21.2 Boolean Equality Operators == and != EXPRESSIONS

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.

For example, -0.0==0.0 is true .

• 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 .

EXPRESSIONS Reference Equality Operators == and != 15.21.3

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. It is a compile-time error 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 (§3.10.5). The contents of two strings s and t can be tested for equality by the method invocation s.equals(t) .