This I read from following link http://www.jusfortechies.com/java/core-java/hashcode.php.
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
I have read similar kins of statement before also. This is very confusing for me. If there are two objects on the heap then each one of them has unique HashCode. If both these objects are equal, then values of their fields are equal, which are compared by overriding equals(). But still they are different objects and have different hashcode values. So what does this statement mean that "hashCode method on each of the two objects must produce the same integer result." ?
I asked similar question before also, but did not get satisfactory replies.
My simple understanding is "for two objects on heap their hash code values are unique, irrespective of their field values. If two objects have same hash code value then that means both references are pointing to same object on heap"
Please correct me if I am wrong.
It prints false, true, true. The two objects are different. But they have the same equals and hash code because they reflect the same value. And String defines objects as equal if they contain the same literal characters in the same order.
Jeanne Boyarsky wrote:A hash code isn't an address in memory.
Quoted For Emphasis.
And to elaborate a bit: Hashcodes were never intended to be unique. They're intended to be a "summary" of the object. The idea is that unequal objects (by equals()) will have unequal hashcodes. It's a way to quickly find the object we're looking for, or quickly determine that it's not present. If our hashing function is well-designed, then unequal objects will be statistically unlikely to have equal hashcodes--but it won't be impossible.
Study up on hash fucntions for more details.
- X 2
neha chaukar wrote:... whilch will return integer value of memory address of both the objects.
Forget about memory addresses. Hash codes do not have (in principle) anything to do with memory addresses. Yes, on the Oracle JVM, Object.hashCode() will return a hash code that's based on the memory address of the object, but that's an unimportant implementation detail, that confuses a lot of people (they think that a hash code is a memory address).
neha chaukar wrote:In the example you show above the third condition will return true only if we override hashcode method .If we do not over ride then hashcode method Object class ...
Note, also though, that the above sample also relies on the .equals() method being overriden so the two different Strings with the same value return true. The default behavior in Object is that .equals() called on two different Objects returns false. Read the API document for equals and hashcode. There is a contract that if you override one then you should override the other so they perform consistently. If you override equals you are supposed to override hashCode to make sure instances that are equal() produce the same hashCode(). It is something you have to make happen not something that happens automatically.