15.21 Equality Operators
Equality operators are syntactically left-associative (they are grouped from left to right), but this fact is essentially never useful; for example, a == b == c analyzes how (a == b) == c. The result type a == b is always boolean, so c must be of type boolean or a compilation error. So a == b == c does not check, a, b and c are equal.
Equality Expression: Relationship Expression EqualityExpression == RelationalExpression EqualityExpression! = RelationalExpression The == (equal) and a = = (not equal) operators are similar to relational operators, except for their lower priority. In this way,
In all cases, a! = B gives the same result as (a == b). Equality operators are commutative if the operand expressions have no side effects.
15.21.1 The operators of numerical equality == and! =
If the operands of the equality operator are both numeric, and one is of the numeric type and the other is convertible (Section 5.1.8) to the numeric type, binary numeric promotion is performed along the operands (Β§5.6.2). If the advanced type of operands is int or long, then an integer equality test is performed; if the advanced type is floating or double, then a floating point equality test is performed. Note that binary numeric promotion performs a conversion on a set of values ββ(Β§5.1.13) and unpacks the conversion (Β§5.1.8). Comparison is performed exactly on floating point values, no matter what value sets their representation, the values ββwere taken from.
Floating-point equality testing is performed in accordance with the IEEE 754 standard:
If either the operand is NaN, then the result == is false, but the result! = Is true. Indeed, the test x! = X is true if and only if the value of x is NaN. (The Float.isNaN and Double.isNaN methods can also be used to check if the value is NaN.) Positive zero and negative zero are considered equal. Therefore, -0.0 == 0.0 is true, for example. Otherwise, two different floating point values ββare considered unequal equality operators. In particular, there is one value representing positive infinity, and one value representing negative infinity; each compares the equal only with itself, and each compares the unequal to all other meanings. Given these considerations of floating point numbers, then for integer operands or operands of a floating point other than NaN: the value created by the == operator is true if the value of the left operand is equal to the value of the right operand; otherwise, the result will be false. The value created by the statement! =, True, if the value of the left operand is not equal to the value of the right operand; otherwise the result will be false. 15.21.2 Logical equality operators == and! =
If the operands of the equality operator are both Boolean, and if one operand is of type boolean and the other is of type Boolean, then the operation is a logical equality. Boolean equality operators are associative. If one of the operands is of type Boolean, it is subjected to unpacking (section 5.1.8).
Result == - true if the operands (after any necessary unpacking conversion) are true or both false; otherwise the result is false.
The result if = is false if both operands are true or both are false; otherwise the result will be true. Thus,! = Behaves in the same way as ^ (Β§15.22.2) with respect to Boolean operands.
15.21.3 equality of references Operators == and! =
If the operands of the equality operator are both references, and the type or null type, then the operation is the equality of the object. a compilation error occurs if it is not possible to convert the type or operand to the type of another by converting customization (Β§5.5). The run-time values ββof the two operands would necessarily be unequal.
At run time, the result == 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 if = = false if the operand values ββare zero or both refer to the same object or array; otherwise the result is correct.
While == can be used to compare references of type String, such an equality test determines whether two operands refer to the same String object. The result is false if the operands are different String objects, even if they contain the same sequence of characters. The contents of the two lines s and t can be checked for equality by calling the s.equals (t) method. See also Β§3.10.5.