Java中空对象的哈希码必须是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21535029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what must be hashcode of null objects in Java?
提问by eagertoLearn
According to a comment from this post, hascode
of null objects
can throw NPE
or a value of zero
. This is implementation specific. but within the same implementation, why does
Objects.hashcode
and hascode(instance)
return different values. for ex:
根据这篇文章的评论,hascode
of null objects
canthrow NPE
或 value of zero
。这是特定于实现的。但在同一个实现中,为什么
Objects.hashcode
和hascode(instance)
返回不同的值。例如:
public class EqualsTesting {
public static void main(String[] args){
String p1 =null;
String p2 = null;
System.out.println(Objects.hashCode(p1));
System.out.println(p2.hashCode());
}
}
Output:
输出:
0
Exception in thread "main" java.lang.NullPointerException
at BinaryTrees.EqualsTesting.main(EqualsTesting.java:14)
If this is the case, will this not affect the key look-up
in HashMap
where null Key-value pairs
are allowed. (It might either hash
to bucket 0
or throw a NPE
)
如果是这样的话,这会不会影响到key look-up
在HashMap
那里null Key-value pairs
是允许的。(它可能既hash
要bucket 0
或throw a NPE
)
采纳答案by Rohit Jain
How would you calculate hashCode
of an object that doesn't even exists? When p2
is null
, invoking any method on it will throw a NPE
. That isn't giving you any particular value of a hashCode.
你将如何计算hashCode
一个甚至不存在的对象?当p2
is 时null
,调用它的任何方法都会抛出一个NPE
. 这并没有为您提供 hashCode 的任何特定值。
Objects.hashCode()
is just a wrapper method, which performs a pre-check for null
values, and for reference that is not null
, it returns the same value as p2.hashCode()
as in this case. Here's the source code of the method:
Objects.hashCode()
只是一个包装器方法,它对值执行预检查null
,对于不是的引用null
,它返回与p2.hashCode()
本例相同的值。下面是该方法的源代码:
public static int hashCode(Object o) {
return o != null ? o.hashCode() : 0;
}
回答by Sanjay T. Sharma
If you will search around, you'll notice that HashMap
has a special handlingfor null
keys. null
values are fine as you don't compute hash code for them in a HashMap
. This is the reason why null
keys work fine in HashMap
. As to why Objects.hashCode
works fine, take a look at Rohit's answer.
如果将搜索周围,你会发现HashMap
有一个特殊处理的null
按键。null
值很好,因为您不在HashMap
. 这就是null
密钥在HashMap
. 至于为什么Objects.hashCode
工作正常,看看 Rohit 的回答。
回答by Happy
As the javadocsays:
正如javadoc所说:
Objects.hashCode(Object o)
Returns the hash code of a non-null argument and 0 for a null argument.
Objects.hashCode(Object o)
返回非空参数的哈希码,返回 0 代表空参数。
p2.hashCode()
throws a NullPointerException
because you are trying to access a method of a null object.
p2.hashCode()
抛出 aNullPointerException
因为您正在尝试访问空对象的方法。
回答by Stephen C
According to a comment from this post, hascode of null objects can throw NPE or a value of zero.
根据这篇文章的评论,空对象的 hascode 可以抛出 NPE 或零值。
That is not true. (And it is not what @Bohemian's comment is saying!)
那不是真的。(这不是@Bohemian 的评论所说的!)
What happens in HashMap
and HashSet
is that they treat null
as a special case. Instead of calling hashcode()
on the null
object (which would NPE!!), they use zero in as a hard-coded alternative hashcode.
发生的事情HashMap
,并HashSet
为他们治疗null
作为一种特殊情况。而不是调用的hashcode()
所述上null
对象(这将NPE !!),它们使用在零作为硬编码的备选哈希码。
I stress ... this is special case behaviour of HashMap
and HashSet
... not hashcode()
.
我强调......这是HashMap
和HashSet
......不是的特殊情况hashcode()
。
As your example shows, if you doattempt to call the hashcode()
method on null
, you willget an NPE. The JLS says that that is what will happen ... and it happens whenever you try to invoke anyinstance method on null
.
如您的示例所示,如果您确实尝试在 上调用该hashcode()
方法null
,您将获得一个 NPE。JLS 说这就是会发生的事情……每当您尝试调用 上的任何实例方法时都会发生这种情况null
。
(On the other hand, the Objects.hashCode(obj)
method doesdeal with the case where obj
is null
as a special case. And that's the whole point of the static method!)
(另一方面,该Objects.hashCode(obj)
方法确实将 where obj
isnull
作为特殊情况处理。这就是静态方法的全部意义所在!)