Reputation: 2678
We known that default implementation of hashCode() returns integer after converting the internal address converting the internal address of the object into an integer . So internal memory of every object is different, then why hashCode() does not generate unique hashcode.
My question is why hashcode() [It returns integer which is address representation of an object] does not generate unique code , if we don't override hashcode() and equals?
Upvotes: 1
Views: 1550
Reputation: 121692
why hashCode() does not generate unique hashcode
Because it has no obligation to do so...
Quoting the javadoc of Object.hashCode()
:
It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results.
And indeed, this is a perfectly legal (even though useless) hashCode implementation:
@Override
public int hashCode()
{
return 42;
}
What is required of the .hashCode()
method is that if two objects are equal according to .equals()
, then they should have the same result for .hashCode()
; and the above method fits the bill!
Mind you, in real life, you would never write such a useless hashCode() method as the above...
Note about the default implementations, that is the ones of Object
:
hashCode()
method calls System.identityHashCode()
;equals()
method tests reference equality (that is, for objects o1
and o2
, it tests that o1 == o2
).If you read the javadoc for Object
carefully, you will see that the contract for both methods is obeyed; The implementation for it is as minimal as you can get, but the contract is obeyed.
Upvotes: 2
Reputation: 34313
Because it can't.
Since there are only 2^32 different ints and there may be more than 2^32 live objects in any VM instance, it is technically impossible to guarantee a unique hash code for each object.
Even if the default hash code may be based on the internal address of the object, it is not identical to the internal address.
Upvotes: 2
Reputation: 3050
The hash code of an object is not its memory address. If you need to use a unique integer for an instance, there's a special method for that called System.identityHashCode(Object)
.
The hash code is just an integer (32 bits) that is meant to give a reasonable approximation of equality. If two hash codes are different, the two objects must be different, but if they are the same they may still be different (though usually they are the same).
This means that if you have two different lists with exactly the same content, they will have the same hash code, but a different memory address.
Upvotes: -1
Reputation: 10239
hashCode only prints the internal memory adress if it is not overridden! (its using the object implementation)
The hash code for a String object is computed as
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation. (The hash value of the empty string is zero.)
Upvotes: 1