您的位置:首页 > 其它

System.identityhashcode()

2014-03-11 00:00 127 查看
今天看到成程序中有这个方法, 有点印象但是又不确认在那里看过啦,做一下笔记。

1. What's the difference between object.hashCode() and System.identityHashCode(object)? I noticed they return different values for the same object.
2. Why would an object return different values when it's passed to System.identityHashCode(object)

The identity hashcode is always the default java.lang.Object implementation, even if the class for a particular object overrides this and computes a different hash code.

The identity hashcode takes no account of the content of the object, just where it is located. The ordinary hashcode may (should) take account of content. Thus, the identity hashcodes for two strings that each contain "hello world" would be different, but the ordinary hashcodes would be the same.
The ordinary hashcode should be coded to be consistent with equals() and compareTo(). The identity hashcode generally isn't consistent with them. In the example above, equals() would return true and compareTo() would return zero, which is consistent with the two strings having the same hash code.

JAVA API

Returns the same hash code for the given object as would be returned by the default method hashCode(), whether or not the given object's class overrides hashCode().
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: