Java Reference
In-Depth Information
The way to test for content equality between two objects is to test whether the values of their
two sets of fields are equal. Notice, however, that the parameter of the equals method is of
type Object , so a test of the fields will make sense only if we are comparing fields of the same
type. This means that we first have to establish that the type of the object passed as a parameter
is the same as that of the object it is being compared with. Here is how we might think of writ-
ing the method in the Student class of the lab-classes project from Chapter 1:
public boolean equals(Object obj)
{
if(this == obj) {
return true; // Reference equality.
}
if(!(obj instanceof Student)) {
return false; // Not the same type.
}
// Gain access to the other student's fields.
Student other = (Student) obj;
return name.equals(other.name) &&
id.equals(other.id) &&
credits == other.credits;
}
The first test is just an efficiency improvement; if the object has been passed a reference to itself
to compare against, then we know that content equality must be true. The second test makes
sure that we are comparing two students. If not, then we decide that the two objects cannot be
equal. Having established that we have another student, we use a cast and another variable of
the right type so that we can access its details properly. Finally, we make use of the fact that
the private elements of an object are directly accessible to an instance of the same class; this is
essential in situations such as this one, because there will not necessarily be accessor methods
defined for every private field in a class. Notice that we have consistently used content-equality
tests rather than reference-equality tests on the object fields name and id .
It will not always be necessary to compare every field in two objects in order to establish that
they are equal. For instance, if we know for certain that every Student is assigned a unique id ,
then we need not test the name and credits fields as well. It would then be possible to reduce
the final statement above to
return id.equals(other.id);
Whenever the equals method is overridden, the hashCode method should also be overridden.
The hashCode method is used by data structures such as HashMap and HashSet to provide ef-
ficient placement and lookup of objects in these collections. Essentially, the hashCode method
returns an integer value that represents an object. From the default implementation in Object ,
distinct objects have distinct hashCode values.
There is an important link between the equals and hashCode methods in that two objects that
are the same as determined by a call to equals must return identical values from hashCode .
This stipulation, or contract, can be found in the description of hashCode in the API documen-
tation of the Object class. 3 It is beyond the scope of this topic to describe in detail a suitable
3 Note that it is not essential that unequal objects always return distinct hash codes.
Search WWH ::




Custom Search