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().
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().
相关文章推荐
- 关于System.identityHashCode(obj) 与 obj.hashcode()
- System的identityHashCode()
- Object.hashCode()方法与System.identityHashCode(object)的区别
- 比较Java中"hashCode" "equals" "==" "System.identityhashcode"
- JDK源码学习--System.identityHashCode(Object)
- 关于System.identityHashCode() 与 obj.hashcode()
- 关于System.identityHashCode(obj) 与 obj.hashcode()
- System.identityHashCode(Object o)
- hashCode和identityHashCode的区别
- hashCode和identityHashCode的区别你知道吗?
- hashCode和identityHashCode的区别
- hashCode和identityHashCode的关系
- hashCode和identityHashCode的区别
- hashCode和identityHashCode的区别
- 两段检验系统生成的identityHashCode是否重复的代码
- 返回对象的唯一标识identityHashCode
- hashCode和identityHashCode 的关系
- hashCode和identityHashCode底层是怎么生成的
- Java学习之equals()、hashCode()、identityHashCode()、toString()和==
- hashCode和identityHashCode的区别