Originally posted by Shallu Shankar:
Hi all ,
o1 and o2 denote two different object references to different objects of same class.
o1.hashCode() == o2.hashCode() will always be false ?
Isn't it true as the hashcode is nothing but the address of the object in the heap .Since these are two different objects therefore hashcode shall be different
Thanks in advance
hi
int hashcode method is defined in Object class ,so every object of a class will have a hashcode ,hashcode is the address of the memory where reference is stored.some classes overrides this method and provide appropriate implementation.for eg
String class .if you make a two object of String class having the same Contents ,then the hashcode return by the objects will be same.
String s1 =new String("Pawan");
String s2 =new String("Pawan");
s1.hashCode() == s2.hashCode()
The hashcode for a String object is computed as
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
see API.
if a class doesnot override a hashcode method then hashcode is determined by using the object hashcode method.
if wrong please correct me.
pawan
------------------