File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes hashCode()???? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "hashCode()????" Watch "hashCode()????" New topic


raymond yadao
Ranch Hand

Joined: Jan 29, 2001
Posts: 88
What does the method hashCode do? can u give some sample code about hashCode()?
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2591

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 -
- Manish
I agree. Here's the link:
subject: hashCode()????
It's not a secret anymore!