• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hashcode

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

------------------
 
reply
    Bookmark Topic Watch Topic
  • New Topic