• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Doubt with hasCode() contract

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is question #52 from K&B Java 6 quiz A



The question is: which of the following will fulfill the equals() and hashCode() contracts for this class?

And correct answers are:
C) return (((SortOf) o).code.length() * ((SortOf) o).bal == this.code.length() * this.bal);
D) return (((SortOf) o).code.length() * ((SortOf) o).bal * ((SortOf) o).rate ==
this.code.length() * this.bal * this.rate);


I can't see why D) is a correct answer. If I try this:


I actually have 2 'equals()' objects in the Set collection. Also, if you read the hashCode() contract, one of the points is:
- if x.equals(y) == true then x.hashCode() == y.hashCode()

and here, with D) declaration of equals(), I have 2 equals() objects with different hashCode()

sortOf1
- equals(): 4 * 3 * 5 = 60
- hashCode(): 12

sortOf2
- equals(): 4 * 5 * 3 = 60
- hashCode(): 4 * 5 = 20


Can you give me a hand?

Thanks in advance.
 
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The rule for equals() and has code is

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.
But the reverse is not necessary, that is If two objects has same has code that doesnt mean they both must be equal


If i am not missing some thing here. the Option C would satisfy this condition, Option D would fail when the vaues of rate and balance are interchanged for two objects.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Hernaez wrote:


I can't see why D) is a correct answer.


D is not a correct answare.
the rule of thumb : equals method should exclude the attribute/instance variables which are not used by hashCode method and vice versa.
 
Daniel Hernaez
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys :-)
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Hernaez wrote:Thank you guys :-)


You are welcome
 
I guess everyone has an angle. Fine, what do you want? Just know that you cannot have this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic