| CHAPTER 15: Expressions |
Previous |
Java Language |
Index |
Next |
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<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 of numeric type, or two operands of type 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) . The equality operators are commutative if the operand expressions have no side effects.
If the operands of an equality operator are both of primitive numeric type, binary numeric promotion is performed on the operands (S5.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.
Floating-point equality testing is performed in accordance with the rules of the IEEE 754 standard:
Subject to these considerations for floating-point numbers, the following rules then hold for integer operands or for floating-point operands other than NaN:
If the operands of an equality operator are both of type boolean , then the operation is boolean equality. The boolean equality operators are associative.
The result of == is true if the operands 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 ^ (S15.21.2) when applied to boolean operands.
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 (S5.4). 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)
(S20.12.9). See also S3.10.5 and S20.12.47.
| © 1996 Sun Microsystems, Inc. All rights reserved. |