如你所知,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/