This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What does hashcode mean? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "What does hashcode mean?" Watch "What does hashcode mean?" New topic

What does hashcode mean?

Mohit Jain
Ranch Hand

Joined: Jun 04, 2007
Posts: 74
Hi Ranchers

What do we understand by an 'object hashcode'. What does an object hashcode represent and how is it calculated.

Please help me to understand this.

SCJP 5.0, SCWCD in progress
David O'Meara

Joined: Mar 06, 2001
Posts: 13459


A hash function is a reproducible method of turning some kind of data into a (relatively) small number that may serve as a digital "fingerprint" of the data. The algorithm "chops and mixes" (i.e., substitutes or transposes) the data to create such fingerprints. The fingerprints are called hash sums, hash values, hash codes or simply hashes. (Note that hashes can also mean the hash functions.) Hash sums are commonly used as indices into hash tables or hash files.

Using hash codes rather than unique values can substantially speed or simplify code.
P Ventura
Ranch Hand

Joined: Jan 24, 2007
Posts: 42
hashcode is used in collections. It's used to find a bucket where your object will be inserted. If you don't understand this, read something about Collections and Maps and all will be clear.

Objective: SCJP 1.5<br /><a href="" target="_blank" rel="nofollow">API</a><br /> <blockquote><font size="1" face="Verdana, Arial">code:</font><hr><pre><font size="2"><br />Double n1 = Double.NaN; Double n2 = Double.NaN;<br /> n1.equals(n2) // true even though Double.NaN != Double.NaN<br />-0.0 == +0.0; // true<br />Double n1 = -0.0;Double n2 = +0.0;<br />n1.equals(n2) // false even though -0.0 == +0.0<br /></font></pre><hr></blockquote>
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15101

See the API documentation for the method java.lang.Object.hashCode():
Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable.

Collection classes such as Hashtable, HashMap and HashSet use hashcodes for efficiency.

The hashCode() method in a class has to adhere to specific rules, which are described in the API documentation for the hashCode() method in class Object. Note that if you write a class with an equals() method, you should also implement the hashCode() method according to the rules.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Stephen Zhang

Joined: Jul 14, 2004
Posts: 11

SCJP 1.4 85%<br />SCWCD 5.0 85%<br />SCBCD 5.0 in progress . . .
marc weber

Joined: Aug 31, 2004
Posts: 11343

Keep in mind that although hashcodes are sometimes considered "digital fingerprints" (as referenced in the Wikipedia article), they are not necessarily unique. Two unequal objects might have the same hashcode and still comply with the hashcode contract (described in the API under Object's hashCode method). On the other hand, equal objects should always return the same hashcode. It's important to understand this distinction for the exam.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
I agree. Here's the link:
subject: What does hashcode mean?
It's not a secret anymore!