• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help for hashCode() method  RSS feed

 
dinesh Venkatesan
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Can anybody clarify me how the hashCode() method works?
My perception about the hashCode() method is it represents the memory location where the object invoking it is stored. Is it correct?

Please clarify me.

Thanks in Advance.

dinesh.
 
Ådne Brunborg
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you could say that. As stated in the Object.hashCode() JavaDoc:

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)


The most important thing to remember about the hashCode method, is that if a.equals(b)==true, then a.hashCode() == b.hashCode() - but the reverse is not necessarily true.

Hope this answers your question?
 
dinesh Venkatesan
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Adane!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that this is the Javadoc for java.lang.Object's hashCode(). Many classes override this method, and their returned values will generally have nothing to do with the object's memory address.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!