0

For example, if I create an A type object,

A a = new A();

then a is a reference on the Stack that points to a A type object on the heap. My question is, if I call a.hashCode(), which one's hash code will be returned, the hashcode of the reference or the hashcode of the object? if it is the hashcode of the object, how can i get the hashcode of the reference? Could anyone kindly give me some tips plz?

2 Answers 2

4

hashCode() is just a non-static method, just like any other non-static method. It's either defined by A, or by a base class of A (Object, in the worst case). All that happens is that method gets called on the instance in question.

how can i get the hashcode of the reference?

You can't, because that doesn't make sense.

0
1

You can get the hash code of the reference by calling:

System.identityHashCode(a);

This is what data structures such as java.util.IdentityHashMap are based on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.