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 K&B master Exam - equal & hashCode contract Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B master Exam - equal & hashCode contract" Watch "K&B master Exam - equal & hashCode contract" New topic
Author

K&B master Exam - equal & hashCode contract

Mumtaz Khan
Ranch Hand

Joined: Feb 21, 2008
Posts: 53
This code snippet is from K&B master exam,
Ques : Which of the following will fullfill the equals() and hashCode() contract for this class
Correct answer Given ,
1) return ((SortOf)o).code.length()*((SortOf)o)*bal == this.code.length()*this.bal; and
2) return ((SortOf)o).code.length()*((SortOf)o)*bal *((SortOf)o)rate == this.code.length()*this.bal * this.rate;

I agree with option 1 but not able to accept option 2. For the below code ,
As per option 2, s1 and s2 are equal but doesn't fullfill the hashCode contract as s1.hashCode != s2.hashCode(). As per the contract if two objects are equal as per equals method then their hashCode must be equal too, isn't it right?
Can some body explain whether option 2 is correct?

SCJP 5.0 -- 97%
David Marco
Ranch Hand

Joined: Feb 23, 2009
Posts: 44
The option 2 contradict the hashCode contract (if two objects are equals according to the equals method, then calling the hashCode() method on each of the two objects must produce the same integer result), but still work well due to the simplistic implementation of equals() and hashCode(). The main purpose for hashCode() is allocate/search an object in a hash structure, and the code above does. Will be equals() who determines if s1.equals(s2) is true according to their implementation. But I really think that answer 2 not apply.


SCJP 6
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 797

Option 1 - if equals result in true then hashCode value always same.
Option 2 - if equals result in false then hashCode may result in same value even if rate value of each object is different.


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
To answer the original question: Yes, Option 2 is incorrect. You simply can't take shortcuts when evaluating whether given hashCode() and equals() implementations obey the mutual contract. Although this case is not an example of it, even if both hashCode() and equals() use the same exact instance variables in their computation, the implementations might not obey the contract.


All code in my posts, unless a source is explicitly mentioned, is my own.
Mumtaz Khan
Ranch Hand

Joined: Feb 21, 2008
Posts: 53
Ruben Soto wrote: even if both hashCode() and equals() use the same exact instance variables in their computation, the implementations might not obey the contract.
Ruben,
Could you please elaborate this statement.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Mumtaz Khan wrote:
Ruben Soto wrote: even if both hashCode() and equals() use the same exact instance variables in their computation, the implementations might not obey the contract.
Ruben,
Could you please elaborate this statement.

Sure thing, Mumtaz. Take for example these two methods:


What happens if one instance has a = 1, b = 0, and another instance has a = 2, b = 0?
Mumtaz Khan
Ranch Hand

Joined: Feb 21, 2008
Posts: 53
So, Isn't the implementation violating the contract , i.e " If two objects are equal by equals method, their hashCode must be same "?
Back to the original question, when the question statement is like "fullfill the equals() and hashCode() contract", should such kind of implementation that violates the contract be chosen? As this question is from K&B, it has confused me about the contract and implementation.


Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
As I already said, Answer 2 should not be correct. If you can find just a single case where two instances are equal but their hashcode values aren't, that means the equals/hashCode contract is not being respected.
Mumtaz Khan
Ranch Hand

Joined: Feb 21, 2008
Posts: 53
Thanks Ruben, I got it
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B master Exam - equal & hashCode contract