gpt4 book ai didi

java - Objects.hashCode() 和 new Object().hashCode() 的区别?

转载 作者:搜寻专家 更新时间:2023-11-01 04:06:13 25 4
gpt4 key购买 nike

这两个代码片段有什么区别?

片段 1:

Object o = new Object();
int i = Objects.hashCode(o);

片段 2:

Object o = new Object();
int i = o.hashCode();

最佳答案

容忍空值

唯一的区别是如果 o 为 null,Objects.hashCode(o) 返回 0 而 o.hashCode() 会抛出一个 NullPointerException.

关于java - Objects.hashCode() 和 new Object().hashCode() 的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16187453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com