It doesn't. The object's hashcodehashCode method is called to do this. See the source code. (line 360).
The code (in the version I linked to) is hashing Strings using a special method. (It appears that this is to allow hashing of strings to be "tuned" at the platform level. I didn't dig into this ...)
The hashcode returned by the
Object.hashcodehashCode()call is "scrambled" further to reduce the chance of collisions. (Read the comment!)
- If
a.equals(ab)thena.hashcodehashCode() == b.hashcodehashCode(). - While
ais in aHashSetor is a key in aHashMap, the value returned bya.hashcodehashCode()must not change. - if
!a.equals(b), then the probability thata.hashcodehashCode() == b.hashcodehashCode()should be low, especially ifaandbare probably hash keys for the application.
Every class inherits a default hashcodehashCode() method from Object (unless this is overridden). It uses what is known as an "identity hash code"; i.e. a hash value that is based on the object's identity (its reference). This matches the default implementation of equals(Object) ... which simply uses == to compare references.
The default hashcodehashCode() method returns the "identity hashcode". This is typically based on the object's memory address at some point time, but it is NOT the object's memory address.
The bottom line is that the default Object.hashcodehashCode() method satisfies all of the requirements that I listed above. It can be relied on.