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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.