Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hashcode() and equals() method relation

 
Mona Gadkari
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
If two objects of an class return same hashcode() are equal
Or when they return true by equals method are they equal.
Or is it necessary that both the above statements be correct for equality of two objects of a class.
mona(varijasmom)
 
Mona Gadkari
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I came across this stmt:
hashCode method is is essential for any class that redefines the equals method. It is required by the general contract for Object.equals. (Equal objects must have equal hash codes.
But this too doesn't give me an explaination regarding my first post on this topic.
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by varijasmom gupte:
hi
If two objects of an class return same hashcode() are equal
Or when they return true by equals method are they equal.
Or is it necessary that both the above statements be correct for equality of two objects of a class.
mona(varijasmom)

Can you rectify your question a bit?
 
Mohana Murali
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if two object are equals return by equals method then their hashcodes must be equal. Where as two hashcodes are equal then those object need not be equal. (They may equal or may not equal it does not matter since it's not a required agreement for hashCode or equals methods).
 
Mona Gadkari
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello
It is preferable that unequal objects have different hashcodes, but that is not always possible. Since the hashcode value is a 32 bit primitive int, it is not possible to produce a unique hashcode for each value of a primitive long.
Now does this mean that two obj which have unequal hashcode may be equal??? pls resolve this confusion of mine.
 
krishna madala
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
might be but need not be..
 
Harwinder Bhatia
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by varijasmom:

Now does this mean that two obj which have unequal hashcode may be equal???

If the hashCode method is implemented properly, the answer to your question is NO.
If equals() returns true for two objects, both objects must return the same integer from the hashCode() method. If hashCode values are different, the equals() method should return 'false'. If equals() returns false, the hashCode values can still be the same.
Hope it helps.
Cheers
Harwinder
 
krishna madala
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry for the wrong post, i didnt read properly,,harwinder is absolutely correct, if 2 objects are equal by equals(), then hashode() must consistanly return the same integer for those 2 objects. but if 2 objects have the same hashcode, then those 2 might be equal or need not be by equals()..
 
Mona Gadkari
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Harwinder and Krishna.....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic