Reputation: 305
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?
Upvotes: 0
Views: 927
Reputation: 215
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.
Upvotes: 2
Reputation: 272687
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.
Upvotes: 4