This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of Refactoring for Software Design Smells: Managing Technical Debt this week in the OO, Patterns, UML and Refactoring forum!

# hashCode Question

Mike Cutter
Ranch Hand

Joined: Jun 09, 2002
Posts: 49
I saw this question on a mock exam, but I don't always trust their answers. I hear that hashCode questions are big on the certification exam, so a thorough explanation of this question would be helpful.
Given two instances of x and y, of a class that correctly implements equals() and hashCode() methods, which two are always equal? (Choose two)
A. (x.equals(y) == false) implies (x.hashCode(y) != y.hashCode())
B. (x.hashCode(y) != y.hashCode()) implies (x.equals(y) == false)
C. (x.hashCode(y) == y.hashCode()) implies (x.equals(y) == y.equals(x))
D. (x.equals(y) == true) implies (x.hashCode() == y.hashCode())
Sudd Ghosh
Ranch Hand

Joined: Oct 23, 2002
Posts: 187
The equals/hashcode contract says:
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(java.lang.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.
Following this, options B and D should be correct.

SCJP 1.4, SCWCD, SCBCD 1.3
Balloon Boo
Greenhorn

Joined: Oct 06, 2002
Posts: 7
I think D must right!en......Maybe B is right!
This afternoon I'll take the exam!GOD bless me!
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Mike Cutter:
A. (x.equals(y) == false) implies (x.hashCode(y) != y.hashCode())
B. (x.hashCode(y) != y.hashCode()) implies (x.equals(y) == false)
C. (x.hashCode(y) == y.hashCode()) implies (x.equals(y) == y.equals(x))
D. (x.equals(y) == true) implies (x.hashCode() == y.hashCode())

The Object.hashCode method does not declare a parameter so the code examples here are problematic.

Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Balloon Boo
Greenhorn

Joined: Oct 06, 2002
Posts: 7
U're right,Dan!
I missed the hashcode() declare!
B is wrong!
Sudd Ghosh
Ranch Hand

Joined: Oct 23, 2002
Posts: 187
Oh, oh... I too overlooked that. If that hashCode parameter was not there, then option B would have been correct, right ? Is there any other problem with that option?
Thanks, Sudd

I agree. Here's the link: http://aspose.com/file-tools

subject: hashCode Question