• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals - hashcode

 
Naresh Gunda
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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
Naresh
 
Gyan Shankar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
False

Two objects can have same hash code but still equals can be false.
However if equals is true hashcode must be true also.
 
Leonardo Rafaeli
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
remember... equals and hashCode can be overloaded...

so, according the "contracts", if I not wrong, if hashCode == hashCode, equal must be equals
 
Tilo Hemp
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Regards,
Sachin
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,...it qould be better if you scribble the table while answering the exam
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic