Java – use system Identityhashcode (obj) – when? Why?

When is the right time to do line 4 instead of line 3? Or do they repeat the call completely?

1  Object o1 = new Object();

2  

3  int hcObj = o1.hashCode();

4  int hcSys = System.identityHashCode(o1);

Solution

Sometimes you may want to create a set of distinguishable objects Some of these objects may be equal to each other, but you still want to refer to all these objects and can only throw real duplicate references You might do this because the equals implementation is not of interest to you (some class overrides are equal when you really don't want them) or because you actually just want to calculate individual instances, etc

To do this (for example, some kind of hash table support), you need an identity based hash code instead of an equal hash code, which is what identityhashcode provides for you It is rarely useful, but it can still be convenient sometimes

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>