• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

equals() , == , hashcode()

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
native int hashcode() :Returns a hash code value for the object.(memory address) .
Boolean equals(Object o):Just does the contents' comparison ..
Equality comparisons(==) : Just does the memory address'(ref.) comparison

Then...how can this following statement is TRUE !!!..Pls..explain.
"If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result. "
THANKS IN ADVANCE.

<marquee> Ratul Banerjee</marquee>
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object class declares an equals() method which checks the memory address
just as == operator. now, if in the class u have created, u don't
override euqals() method then the Object class version is called,
which compares the addresses.the hashcode() method in the Object class
returns the "class name"+@+hexadecimal representation of object.
this is unique to each object.hence, if u don't override the hashcode()
method in ur class and use the default version of the Object class,
then it would return same values only for references pointing to the
same object.
"Boolean equals(Object o):Just does the contents' comparison .. "
equals methods implementation is in ur hands.though the content comparison
that u r stating above is for the String and StringBuffer classes.
also remember one more thing :
Float f=new Float("2f");
Integer i=new Integer("2");
i.equals(f) would return false because the equals() method in wrapper
classes are overridden so as to check the class also.
correct me if i am wrong.
 
kriti sharma
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
did u understand it ratul???
 
ratul banji
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi kriti,
Thank u very much.
YEs...I have understood..I had some mis-concepts
...now ..It has been cleared by u.
Thanks 1ce again.
<marquee> Ratul . </marquee>
[This message has been edited by ratul banji (edited April 04, 2001).]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic