Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hashcode and equals

 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
k&b says that equals and hashcode should use the same instance variables...
hashcode and the equals method should use the same instance variables


but the masterexam says that equals method must be atleast as precise as the hashcode method is..beacuse in the question equals uses more instacne varibales htan the hashocde method.now which one is right?
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The official contract says that when 2 objects are equal, they have to have the same hashcode.

On the other hand, if two objects have the same hashcode, they don't have to be equal to each other.

That's the only big rule you have to stick to.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's something to say about both statements. The equals method must return true when 2 objects are meaningful equal. Therefor not even all the instance variables have to be used.
Just follow these simple rules and you will be fine:

Equals must be:

Reflexive: x.equals(x) == true
Symmetric: x.equals(y) == true then y.equals(x) == true
Transitive: x.equals(y) == true and y.equals(z) == true then z.equals(x) == true
Consistent: Multiple calls must return the same result as long as none of the instance variables used in the equals method changed.

Hashcode:
x.equals(y) == true then x.hashCode() == y.hashCode()
x.equals(y) == false then x.hashCode() != y.hashCode()
This can not be mirrored. So when x.hashCode == y.hashCode, x.equals(y) does NOT have to be true
 
rushikesh sawant
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
using equals() method without overriding, no two objects will ever be equal. because equals check that if two references are referring to the same objects.

equals() is a check for equality on two references while hashCode() is called to get hashcode value of one object.
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raju Champaklal wrote:k&b says that equals and hashcode should use the same instance variables...
hashcode and the equals method should use the same instance variables


but the masterexam says that equals method must be atleast as precise as the hashcode method is..beacuse in the question equals uses more instacne varibales htan the hashocde method.now which one is right?


k&B says they should, not must use same instance variables that means you can use other instance variables also while implementing equals method. But because it has to be atleast as precise as hashcode so you must use those instance variables used in hashcode method.
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh never thought grammar would be such a hindrance to the exam....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic