The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes difference between hashCode and equals method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "difference between hashCode and equals method" Watch "difference between hashCode and equals method" New topic

difference between hashCode and equals method

Sujittt Tripathyrr
Ranch Hand

Joined: Jun 21, 2006
Posts: 96

Can you please tell me what is the basic difference between hashCode and equal method.

Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
Try to understand why we need hashCode method and why we need
equals method and how they are interlinked.
Try to understand how equals and hashcode method are used with
Then you will be able to understand difference and similarities between
two methods.
Bod Toki
Ranch Hand

Joined: Sep 01, 2006
Posts: 95
here's an easy way to sort out the difference

...we want to sort out a couple of balls- so it's easy to find them later. some red, some blue, some green, etc...

20 balls in all (labeled 1 - 20, each ball has a unique number)
5 greens (1 - 5)
5 reds (6 - 10)
5 blues (11 - 15)
5 yellows (16 - 20)

...what we do is pick a ball from our pile- if it's red, it goes in our red bucket, blues in blue bucket, etc...

...if the two of us have that agreement (ie. me sorting the balls into their relative colored buckets and you coming for a ball later), then you can come later on (after i have left) and find it an easy task to find that red ball (say marked number 9) that you are looking for. here's how it relates to java...

the colored buckets- quick way for you to know where the ball you are looking for is.

that unique number (remember 1 - 20) that identifies the exact ball you are looking for.

if the methods are implemented properly (yes -some wack programmers don't implement them properly - be sure you will see this on the exam) then if we are looking for the no. 9 ball (from our earlier chart), the ball must be red and it must be in the red bucket.

a no. 6 - 10 ball is red

a red ball is not necessarily a no. 9 ball-> figure that out :roll:
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257


The hashcode is a unique number to identify the object. Mostly we use hashcode to identify a particular object. Let stake the following situation...

We are classifying the objects one some attribute and all objects which are mathcing on one attribute will be placed in one queue and like that ....

First all objects with same hascode are put in single line and to differentiate with in single line we use the equlas.

So for any given two objects,

if hashcode is same then it is not neccessary that two objects are equal.
if they are equal then their hashcode must be same.

Try some hands on work ....

More inputs are welcome....
Cosima Laube

Joined: Aug 19, 2006
Posts: 3
Hey Bod,

thanks a lot for taking the effort of writing down this scenario. Now I have a picture in mind about the difference between hashCode and equals. :-)

With regards,
[ September 02, 2006: Message edited by: Cosima Laube ]
I agree. Here's the link:
subject: difference between hashCode and equals method
It's not a secret anymore!