java.util.Objects.isNull vs object == null [英] java.util.Objects.isNull vs object == null
问题描述
如您所知, java.util.Objects
As you know, java.util.Objects
is
此类包含用于对象操作的静态实用程序方法。
This class consists of static utility methods for operating on objects.
其中一种方法是 Objects.isNull()
。
我的理解是 Objects.isNull()
将通过省略对象来消除意外为对象分配空值的机会第二个 =
。
My understanding is that Objects.isNull()
would remove the chance of accidentally assigning a null value to object by omitting the second =
.
但是,API Note说明:
However, the API Note states:
此方法可用作谓词,过滤器(Objects :: isNull)
This method exists to be used as a Predicate, filter(Objects::isNull)
是否有任何理由/情况我应该使用 object == null
而不是 Objects.isNull()
if语句?
Would there be any reason/circumstance for which I should use object == null
over Objects.isNull()
in an if statement?
应该 Objects.isNull()
仅限于Predicates吗?
Should Objects.isNull()
be confined to Predicates exclusively?
推荐答案
应该在if语句中对Objects.isNull()使用object == null吗?
should use object == null over Objects.isNull() in a if statement?
如果你看一下源代码 IsNull
方法,
/* Returns true if the provided reference is null otherwise returns false.*/
public static boolean isNull(Object obj) {
return obj == null;
}
它是一样的。没有区别。所以你可以安全地使用它。
It is the same. There is no difference. So you can use it safely.
这篇关于java.util.Objects.isNull vs object == null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!