• 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

hashcode() and equals() method relation

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
might be but need not be..
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Harwinder and Krishna.....
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic