File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hashCode() doubt 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 "hashCode() doubt" Watch "hashCode() doubt" New topic

hashCode() doubt

raja kanak
Ranch Hand

Joined: Oct 18, 2006
Posts: 135
I got this mock question from JQPlus

I guess a,b,c,d are correct. But the answer is a,b,d,e
My doubt is how c is correct? I can't understand the logic?

Thanks for any help.

Matthew Alesi
Ranch Hand

Joined: Sep 13, 2006
Posts: 38
This is because choice c would violate the unchecked contract that equals() and hashCode() must fulfill in order for them to work properly.

One of the stipulations of this contract is that hashCode() and equals() should use the same criteria to formulate their values, or at least that equals() use all of what hashCode uses and then some more if you want. Think about it: if hashCode() came up with two different hashcodes based on different values for b in two objects of the above class, the equals() test might still return true if the a values are the same. hashCode()'s must be equal if equals() is true.

I hope I explained that well enough. Please say so if I didn't.

Current CS undergrad
SCJP 5.0
raja kanak
Ranch Hand

Joined: Oct 18, 2006
Posts: 135
Thanks Matthew Alesi.
Now i understand the issue.
I agree. Here's the link:
subject: hashCode() doubt
It's not a secret anymore!