Java Array HashCode implementation
The java.lang.Array
hashCode
method is inherited from Object
, which means the hashcode depends on the reference. To get the hashcode based on the content of the array use Arrays.hashCode
.
Beware though its a shallow hashcode implementation. A deep implementation is also present Arrays.deepHashCode
.
Arrays use the default hash code, which is based on memory location (but it isn't necessarily the memory location, since it's only an int
and all memory addresses won't fit). You can see this by also printing the result of System.identityHashCode(foo)
.
Arrays are only equal
if they are the same, identical array. So, array hash codes will only be equal, generally, if they are the same, identical array.
The default implementation for Object.hashCode() is indeed to return the pointer value of the object, although this is implementation dependent. For instance, a 64-bit JVM may take the pointer and XOR and high and low order words together. Subclasses are encouraged to override this behavior if it makes sense.
However, it does not make sense to perform equality comparisons on mutatable arrays. If an element changes, then the two are no longer equal. To maintain the invariant that the same array will always return the same hashCode no matter what happens to its elements, arrays do not override the default hashcode behavior.
Note that java.util.Arrays provides a deepHashCode() implementation for when hashing based on the contents of the array, rather than the identity of the array itself, is important.