wood burning stoves*
The moose likes Beginning Java and the fly likes difference between hashCode and equals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "difference between hashCode and equals" Watch "difference between hashCode and equals" New topic
Author

difference between hashCode and equals

anudeep jain
Greenhorn

Joined: Jun 23, 2006
Posts: 1
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.
Nikhil Goel
Ranch Hand

Joined: Jul 11, 2003
Posts: 32
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
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 ]
    Ramasubbhu Allur Kuppusamy
    Ranch Hand

    Joined: Sep 16, 2005
    Posts: 120
    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.


    Regards,<br />Ram.<br />SCJP 1.4
    S Thanigaivel
    Ranch Hand

    Joined: Oct 06, 2005
    Posts: 60
    Can anyone explain this concept with an example?

    Thanks
    Ilja Preuss
    author
    Sheriff

    Joined: Jul 11, 2001
    Posts: 14112
    Found this one via google: http://www.geocities.com/technofundo/tech/java/equalhash.html


    The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
    Kuldeep Singh DH
    Greenhorn

    Joined: Apr 02, 2009
    Posts: 2
    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.
    Amit Ghorpade
    Bartender

    Joined: Jun 06, 2007
    Posts: 2716
        
        6

    "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.


    SCJP, SCWCD.
    |Asking Good Questions|
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37884
        
      22
    And why have you reopened a 3-year-old thread?
    Kuldeep Singh DH
    Greenhorn

    Joined: Apr 02, 2009
    Posts: 2
    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
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: difference between hashCode and equals
     
    Similar Threads
    Difference between Hashcode and Equals() method?
    What is hashCode?
    equals() and hashCode()
    Difference between hashCode() and equals()
    What's the difference between hashcode() and equals() methods?