File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hashCode() doubt" Watch "hashCode() doubt" New topic
Author

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.


live
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.


-Matt
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: http://aspose.com/file-tools
 
subject: hashCode() doubt
 
Similar Threads
Understanding hashcode()
null doubt
Hash code
how to solve this hashcode() example.?
Enthuware mock test question (hashCode)