This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Hash Code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Hash Code" Watch "Hash Code" New topic
Author

Hash Code

shruti patel
Ranch Hand

Joined: Mar 10, 2008
Posts: 52
if two references are having same hash codes,is that means
those are referring to same object?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

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 ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
Consider Paul's rocket mass heater.
 
subject: Hash Code
 
Similar Threads
K&B doubt in hashcode() and equals(): Is my understanding correct ?
About hascode()....
Decoding MD5 hashcode
Duplicate elements in HashSet
Hashmap