what must be hashcode of null objects in Java?

后端 未结 4 1395
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-19 23:14

According to a comment from this post, hascode of null objects can throw NPE or a value of zero. This is implementation speci

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-19 23:47

    As the javadoc says:

    Objects.hashCode(Object o)

    Returns the hash code of a non-null argument and 0 for a null argument.

    p2.hashCode() throws a NullPointerException because you are trying to access a method of a null object.

提交回复
热议问题