
5.9 Comparing Objects 245
5.9 Comparing Objects
5.9.1 The equals Method
Often, you’ll want to compare whether two objects are equal; typically, we
will say that two objects are equal if they have the same data. If you use the
equality operator (
==) to compare object references, however, you are com-
paring the value of the object references. In other words, you are compar-
ing whether the object references point to the same object, that is, the same
memory location. To compare object data, you need to use the equals
method, which all classes inherit from the Object class. We will cover inher-
itance in greater detail in Chapter 10. Many classes provide ...