• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Hash Code  RSS feed

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if two references are having same hash codes,is that means
those are referring to same object?
 
Sheriff
Posts: 21503
96
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!