How come hashcodes can be the same even if objects aren't equal?

Questions by krishna_darl   answers by krishna_darl

Showing Answers 1 - 5 of 5 Answers

satishm

  • May 26th, 2006
 

If you override equals() method and say { return false; } , even same objects will not be equal. If that was not the trick question the interviewer meant, then I think that if there are two different objects, even though they may have same values contained, their hashcodes cannot be the same. I am not sure though. I have never seen two different objects having the same hashcode, while I have experienced same objects (held by two different references) returning same hashcode.

Eg. Integer i = new Integer(8745);Integer j = new Integer(8745); Here i and j , though containing same value will give you diff hashcode.

Integer i = new Integer(1); Integer j = i; j and i will give you the same hashcode.

  Was this answer useful?  Yes

Anilkumar

  • Nov 2nd, 2006
 

Hi...Friends, If two Objects are equal then the hashcode must be same for those Objects... If two Hashcodes are same for two objects then those objects may not equal...... Thank You...

  Was this answer useful?  Yes

sandhya

  • Apr 16th, 2007
 


     what i feel according to me hashcodes are unique the objects can be same but not the hashcode because they are the key values with which we retrieve the obiects ,However as hashtable can have duplicate values there is  a possibilty of having the same hashcodes

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.