This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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 ]
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.
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
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.
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.