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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:13:17  来源:igfitidea点击:

what must be hashcode of null objects in Java?

javaobjecthashmaphashcode

提问by eagertoLearn

According to a comment from this post, hascodeof null objectscan throw NPEor a value of zero. This is implementation specific. but within the same implementation, why does Objects.hashcodeand hascode(instance)return different values. for ex:

根据这篇文章的评论,hascodeof null objectscanthrow NPE或 value of zero。这是特定于实现的。但在同一个实现中,为什么 Objects.hashcodehascode(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-upin HashMapwhere null Key-value pairsare allowed. (It might either hashto bucket 0or throw a NPE)

如果是这样的话,这会不会影响到key look-upHashMap那里null Key-value pairs是允许的。(它可能既hashbucket 0throw a NPE

采纳答案by Rohit Jain

How would you calculate hashCodeof an object that doesn't even exists? When p2is null, invoking any method on it will throw a NPE. That isn't giving you any particular value of a hashCode.

你将如何计算hashCode一个甚至不存在的对象?当p2is 时null,调用它的任何方法都会抛出一个NPE. 这并没有为您提供 hashCode 的任何特定值。

Objects.hashCode()is just a wrapper method, which performs a pre-check for nullvalues, 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 HashMaphas a special handlingfor nullkeys. nullvalues are fine as you don't compute hash code for them in a HashMap. This is the reason why nullkeys work fine in HashMap. As to why Objects.hashCodeworks 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 NullPointerExceptionbecause 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 HashMapand HashSetis that they treat nullas a special case. Instead of calling hashcode()on the nullobject (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 HashMapand HashSet... not hashcode().

我强调......这是HashMapHashSet......不是的特殊情况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 objis nullas a special case. And that's the whole point of the static method!)

(另一方面,该Objects.hashCode(obj)方法确实将 where objisnull作为特殊情况处理。这就是静态方法的全部意义所在!)