Java Reference
In-Depth Information
Only the equality operators == and != are allowed to operate on boolean
values.
These operators can be combined to create a "logical XOR " test. The fol-
lowing code invokes sameSign only if both x and y have the same sign (or
zero); otherwise, it invokes differentSign :
if ((x < 0) == (y < 0))
sameSign();
else
differentSign();
The equality operators can also be applied to reference types. The ex-
pression ref1==ref2 is true if the two references refer to the same object
or if both are null , even if the two references are of different declared
types. Otherwise, it is false.
The equality operators test for reference identity, not object equivalen-
ce. Two references are identical if they refer to the same object; two
objects are equivalent if they logically have the same value. Equivalen-
ce is tested with the equals method defined by Object , which you should
override in classes for which equivalence and identity are different. Ob-
ject.equals assumes an object is equal only to itself. For example, the
String class overrides equals to test whether two String objects have the
same contentssee Chapter 13 .
9.2.3. Logical Operators
The logical operators combine boolean expressions to yield boolean val-
ues and provide the common operations of boolean algebra:
logical AND
&
 
Search WWH ::




Custom Search