Forums Register Login

Hash Code

+Pie Number of slices to send: Send
if two references are having same hash codes,is that means
those are referring to same object?
+Pie Number of slices to send: Send
Not necessarily.

It all depends on the implementation of the hashCode() method. Object returns an identity hash code, so if that implementation is used the answer is (most likely) yes.

However, I could override the hashCode() method to return a constant, e.g. 42. Now all objects of my class will have the same hash code. Granted, it's an unpracticle example, but there may be implementations that may lead to similar hash codes for different objects.


The only things you can tell about two objects if you only have their hash codes:
- if the hash codes are different the objects are different
- if the hash codes are the same the objects might be equal


Also read the API for Object.equals and Object.hashCode - it's an interesting read.
[ August 12, 2008: Message edited by: Rob Prime ]
I am Arthur, King of the Britons. And this is a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 913 times.
Similar Threads
Duplicate elements in HashSet
Decoding MD5 hashcode
Hashmap
About hascode()....
K&B doubt in hashcode() and equals(): Is my understanding correct ?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 05:21:39.