• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hashCode()????

 
raymond yadao
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the method hashCode do? can u give some sample code about hashCode()?
thnks....
raymond
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It simply returns an integer that can be used as a hash code for that particular object, which can be used by the Java collection classes such as Hashtable.
Moreover, it is required that if objects are equal according to the equals(Object obj) method, the hash code returned by them must be same.
As for examples, in standard Java wrapper class Integer, if you construct an object of class Integer such as -
Integer seven = new Integer(7);
and call seven.hashCode() it will return 7.
You can read about how to implement hashCode() method in your own classses at - http://developer.java.sun.com/developer/Books/effectivejava/Chapter3.pdf
HTH,
- Manish
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic