File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equals - hashcode 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 "equals - hashcode" Watch "equals - hashcode" New topic

equals - hashcode

Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
Can any one of you explain this question with an example pls?

"If two objects return the same hashCode value they must be equal according to the equals method"
A) True B) False

Thanx & Regards
Gyan Shankar
Ranch Hand

Joined: Dec 12, 2005
Posts: 65

Two objects can have same hash code but still equals can be false.
However if equals is true hashcode must be true also.

SCJP(1.4), SCWCD(1.4), SCBCD(1.3), SCDJWS
Leonardo Rafaeli
Ranch Hand

Joined: Dec 12, 2005
Posts: 58
remember... equals and hashCode can be overloaded...

so, according the "contracts", if I not wrong, if hashCode == hashCode, equal must be equals

SCJP 1.4, SCWCD 1.4, SCBCD 5, OCUP, SCEA 5 (I), Oracle SOA Implementation Specialist, IBM Websphere Lombardi 7.1 Fundamentals
Blog: [url][/url]
Tilo Hemp
Ranch Hand

Joined: Nov 21, 2005
Posts: 91
so, according the "contracts", if I not wrong, if hashCode == hashCode, equal must be equals

it's the other way round:
if a.equals(b) then a.hashCode() == b.hashCode()

The hash code is some kind of very reduced "coarse id" of the object. Obviously, an int can not hold in all cases all information needed to distinguish the object from all other instances for all time.

E.g. a simple class with two int member variables can have 2^64 different instanciations, but a single integer can only be used to distinguish between 2^32 of them.
[ December 20, 2005: Message edited by: Tilo Hemp ]
NareshAnkuskani Kumar
Ranch Hand

Joined: Sep 15, 2005
Posts: 76
the hash code is used to determine the bucket( the place to store the keys)

suppose u have two objects whose hashcodes are equal but the objects are not equal as per the equals method

at this time first when u store the first object
first the bucket is determined using the hashcode of this object,later the object is stored.
when u store the second object similarly the hashcode of this object is determined since it has the same hashcode previously it checks wheather the object being stored are equal
if true then it replaces the one which is stored first.
if false then it stores the object in the same bucket, but while retriving the value first bucket is determined, since in this bucket there are two objects it uses the equals method to retrieve the value.this is legal, but this is inefficient in programming style.
hope u got the solution
if not query me about u r doughts
Sachin Dimble
Ranch Hand

Joined: Dec 07, 2005
Posts: 100
Want to add something-
hash code is computed according to the following formula:

s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation. (The hash value of the empty string is zero.)

Does the same tech. used for any kind of objects.

Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
for answering such questions make sure that you cram up the table on K&B book page 418. in the real exam you wont get too much time to think on such questions, qould be better if you scribble the table while answering the exam

SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
I agree. Here's the link:
subject: equals - hashcode
It's not a secret anymore!