jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in hashCode() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in hashCode()" Watch "Doubt in hashCode()" New topic

Doubt in hashCode()

Adil El mouden
Ranch Hand

Joined: Jun 01, 2005
Posts: 82
This is a question found in the mock exam of mughal book:

Given the following class, which are correct implementations of the hashCode() method?

class ValuePair {
public int a, b;
public boolean equals(Object other) {
try {
ValuePair o = (ValuePair) other;
return (a == o.a && b == o.b)
|| (a == o.b && b == o.a);
} catch (ClassCastException cce) {
return false;
public int hashCode() {
// Provide implementation here.

Select the three correct answers.

a)return 0;

b)return a;

c)return a + b;

d)return a - b;

e)return a ^ b;

f)return (a << 16) | b;

answer are a, c, e

If answer a is true why then answer b it isnt?
Thank you.

SCWCD 1.4(Loading...), SCJP 1.4(98%), Bachelor of Engineering (computer science)
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
If two objects are equal according to the equals method, then they must have the same hashcode value in order to satisfy the equals-hashcode contract. Consider this situation:

You have a ValuePair object with a=2 and b=7, and you have another ValuePair object with a=7 and b=2. According to the equals method, these two objects are equal. Therefore, according to the contract, these two objects must return the same value from their hashCode() methods.

But if we choose return a; for the body of the hashCode() method, then one object will have a hashcode of 2, and the other will have a hashcode of 7, so we've broken the contract by writing an incorrect set of hashCode() and equals() implementations. If we chose return 0; then the two objects would have the same hashcode value, so that implementation would be correct.

SCJA 1.0 (98%), SCJP 1.4 (98%)
Adil El mouden
Ranch Hand

Joined: Jun 01, 2005
Posts: 82
Thank you Joseph Sondow
Adil El mouden
Ranch Hand

Joined: Jun 01, 2005
Posts: 82
Please can you explain why answer F is incorrect.
Thank you.
Philip Heller
Ranch Hand

Joined: Oct 24, 2000
Posts: 119
Why isn't (F) an okay hash code?

Suppose a is 3 and b is 5. Then the hash code is (3 << 16) | 5. In hex this is 30005. Now consider a different instance, where a is 5 and be is 3. The hash code is (5 << 16) | 3, which in hex is 50003.

But the equals() method returns true when comparing these two objects, so the hash code contract is violated.

-- Phil

Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".
janki tangeda
Ranch Hand

Joined: Jun 07, 2005
Posts: 54
F is incorrect because the expression in F evaluates to two different values when a=o.b & b=o.a.that means different hashcodes for object that are equal according to equals().thus it violates the equals() contract.
Adil El mouden
Ranch Hand

Joined: Jun 01, 2005
Posts: 82
Many Thanks to Philip Heller.
I'm lucky!

And to janki tangeda too.
I agree. Here's the link: http://aspose.com/file-tools
subject: Doubt in hashCode()
It's not a secret anymore!