本文介绍了java.util.Objects.isNull vs object == null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如您所知,

As you know, java.util.Objects is

其中一种方法是 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:

是否有任何理由/情况我应该使用 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?

推荐答案

如果你看一下 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 22:39