gpt4 book ai didi

java.util.Objects.isNull vs object == null

转载 作者:IT老高 更新时间:2023-10-28 20:31:07 27 4
gpt4 key购买 nike

如你所知,java.util.Objects

This class consists of static utility methods for operating on objects.

其中一种方法是 Objects.isNull()

我的理解是 Objects.isNull() 会通过省略第二个 = 来消除意外为对象分配空值的机会。

但是,API 说明指出:

This method exists to be used as a Predicate, filter(Objects::isNull)

是否有任何理由/情况我应该在 if 语句中使用 object == null 而不是 Objects.isNull() ?

Objects.isNull() 是否应该仅限于 Predicates?

最佳答案

should use object == null over Objects.isNull() in a if statement?

如果您查看 source code IsNull 方法,

 /* Returns true if the provided reference is null otherwise returns false.*/

public static boolean isNull(Object obj) {
return obj == null;
}

是一样的。没有区别。这样您就可以放心使用了。

关于java.util.Objects.isNull vs object == null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37972859/

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