6.2 Overriding equals() & hashcode()Homepage  « Java5 Certification « 6.2 Overriding equals() & hashcode()

The equals() method of the Object class only ever returns true when the reference variables refer to the same object which isn't helpful when we want our collections sorted and stored dependent upon fields in the collection. So in this lesson we look at correct and incorrect overrides of corresponding equals() and hashCode() methods and explain the difference between == and the equals() method.

Lets take a look at the points outlined at the Oracle Website for this part of the certification.

  • Section 6: Collections / Generics

    • Distinguish between correct and incorrect overrides of corresponding hashCode and equals methods, and explain the difference between == and the equals method.

Checking Object Equalitygo to top of page Top

The equals() method of the Object class indicates whether another object is equal to the invoking object by using the == relational operator. What the method is actually doing is checking the bits in both reference variables and returning a boolean value. Therefore the equals() method of the Object class only ever returns true when the reference variables refer to the same object. Following is the contract specified in the Oracle documentation for the equals() method of the Object class:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

Overriding equals()go to top of page Top

But what if we want to think of two objects as equal if part of their object state is shared? Well then we need to override the equals() method of the Object class.

See OO Concepts - Overriding equals() for example code.

The hashCode() Methodgo to top of page Top

The hashCode() method returns the hash code value associated with the object. Following is the contract specified in the Oracle documentation for the hashCode() method of the Object class:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode() method must consistently return the same integer, provided no information used in equals() comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals(Object) method, then calling the hashCode() method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals(Object) method, then calling the hashCode() method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.

From this contract we can see that the hashCode() method of the Object class, relies on the equals() method of the Object class. What this means to us is whenever we override the equals method of the Object class, we should also override the hashCode() method to keep the general contract for the hashCode() method, which states that equal objects must have equal hash codes. We can tabularize the association between the equals and hashCode() contracts to make understanding easier:

Condition Mandatory Not Mandatory But Possible
From the equals() viewpoint
if x.equals(y) returns truex.hashCode() == y.hashCode()
if x.equals(y) returns falseNo hashCode() requirement
From the hashCode() viewpoint
x.hashCode() == y.hashCode()x.equals(y) returns true
x.hashCode() != y.hashCode()x.equals(y) returns false

See OO Concepts - Overriding hashCode() for example code.

Related Java5 Tutorials

OO Concepts - The Object Superclass

<<  6.1 Collection Usage                    6.3-6.4 Generic Types  >>

go to home page Java5 Tutor Homepage go to home page Top