• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

K&B master Exam - equal & hashCode contract

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mumtaz Khan
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ruben, I got it
 
They worship nothing. They say it's because nothing lasts forever. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic