Java:object.hashCode()和Objects.hashCode(object)

时间:2016-08-12 12:45:55

标签: java equals hashcode

一个简单的简短问题:object.hashCode()Objects.hashCode(object)是否相等?有什么不同?他们是否从对象中计算相同的哈希值?

2 个答案:

答案 0 :(得分:4)

正如你在实施中所看到的那样

public static int hashCode(Object o) {
    return o != null ? o.hashCode() : 0;
}

是肯定的。如果对象o为空,它会阻止NPE。

答案 1 :(得分:3)

如果Objects.hashCode(object)object

null将返回零。

在这种情况下,NullPointerException将被object.hashCode()抛出。

对于非null引用,函数是等效的。

相关问题