• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

difference between hashCode and equals

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
Can anybody explain me the difference between hashCode() and equals(). I have read it from book but not quiet clear how to use them in programs. If possible please give some example also.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HashCode and equals are entirely 2 diff methods on Object class in JAVA . hasgcode returns a unique indentifier for the object while equals is used for comparison of 2 objects .
equals method can be overridden by all custom objects for there equality comparison . By default equals method just compares the references.
Hope it helps.
 
Marshal
Posts: 80266
430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Slightly more precisely (or pedantically).
Equals is used to see whether the details of two objects are the same. If you are using a class seriously, rather than as a training exercise, you ought to override the equals method. You should return true if and only if
  • The two objects are from exactly the same class
  • and all their fields have the same values.


  • As a shortcut, you can return true if object1 == object2 because if they are the same object, they ought to have the same features.
    If you override the equals method, you must override the hashcode method too, because two objects which return true to their equals method must return the same hashcode. More details in the java.lang.Object class in the API specification.
    [ June 27, 2006: Message edited by: Campbell Ritchie ]
     
    Ranch Hand
    Posts: 120
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Furthermore, there is a relationship between equals() method and the hashCode() method that you can't afford to overlook.
    Note that it is generally necessary to override the hashCode() method whenever equals() method is overridden, so as to maintain the general contract for the hashCode method, which states :
    Two equal objects must have/produce equal hash codes.
    However, two unequal objects need not produce distinct hash codes.
    If hashCodes of the two objects are not equal, then it implies that those two objects are not equal.
     
    Ranch Hand
    Posts: 60
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Can anyone explain this concept with an example?

    Thanks
     
    author
    Posts: 14112
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Found this one via google: http://www.geocities.com/technofundo/tech/java/equalhash.html
     
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The JAVA API documentation states that if two objects are equal, then they must have the same hash code, however the opposite is NOT true. But, when both functions run independently, then why this contract?

    There are some myths that equals() calls hashcode() internally or vice-versa, but this is not truth. use of equals is straight forward, and hashCode () is used only for hashing capabilities in hash capable collections(i.e HashMap, HashSet) in java.

    hashcode() is used to generate 'hash' which is number that is used to index/locate a memory location in a large list of memories("bucket"). So all the equal objects should be found in same bucket so all equal object should have same hashcode. but opposite is not true because a bucket can contains different objects.
     
    Bartender
    Posts: 2856
    10
    Firefox Browser Fedora Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    "Kuldeep Singh DH " welcome to Javaranch
    please check your private messages for an important administrative matter. You can check them by clicking the My Private Messages link above.
     
    Campbell Ritchie
    Marshal
    Posts: 80266
    430
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    And why have you reopened a 3-year-old thread?
     
    Kuldeep Singh DH
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    oh ok sorry for that. actually I used JavaRanch for the first time and I find this question. and It promt me to share my thoughts here. Now I read "How To Answer Questions On Java Ranch". I will take care in future.

    But this does not only helpful for the user who actually posted this question but also it helps the people who are new in java and have similar kind of quesion now. Its just my thought. anyway I will care this in future.

    Thanks
    Kuldeep
     
    The longest recorded flight time of a chicken is 13 seconds. But that was done without this tiny ad:
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic