hash value in java objects

Krystal Mok rednaxelafx at gmail.com
Fri Dec 7 20:38:46 PST 2012

Short answer is the identity hash code is calculated when:
1. the hashCode() method is called the first time for this object, if
the type of this object doesn't override Object.hashCode();
2. System.identityHashCode() is called the first time for this object.

A "0" value encoded as the hash in the mark word indicates that the
identity hash code hasn't been calculated yet (which is the initial
state after an object is created).

If the type of an object overrides Object.hashCode(), then calling it
doesn't have anything to do with the identity hash code.

The identity hash code of a Java object is stored in the mark word of
the object header if it has been calculated and the object is
unlocked. Otherwise it may be stored in the displaced mark word or in
the inflated lock.

- Kris

On Sat, Dec 8, 2012 at 6:31 AM, Vitaly Davidovich <vitalyd at gmail.com> wrote:
> Hash is encoded in the mark word, so I think the answer to your question is
> it's created when object is allocated (and thus gets a markOop).  The actual
> hash value is decoded from the mark word at call time, but it's just a shift
> and mask at that point.  Someone can correct me if this is wrong.
> Thanks
> Sent from my phone
> On Dec 7, 2012 4:52 PM, "Xin Tong" <xerox.time.tech at gmail.com> wrote:
>> I am wondering when the hash values in the java objects are
>> materialized. are they materialized when the object is created ? or
>> when hashCode on the object is called.  what about modifications to
>> the object ?
>> Xin

More information about the hotspot-compiler-dev mailing list