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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37972859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:51:34  来源:igfitidea点击:

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

javajava-8compare

提问by Lucas T

As you know, java.util.Objectsis

如你所知,java.util.Objects

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

此类包含用于操作对象的静态实用程序方法。

One of such methods 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 =.

我的理解是,Objects.isNull()通过省略第二个=.

However, the API Note states:

但是,API 说明指出:

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

此方法存在用作谓词,filter(Objects::isNull)

Would there be any reason/circumstance for which I should use object == nullover Objects.isNull()in an if statement?

我应该在if 语句中使用object == nullover 的任何原因/情况吗?Objects.isNull()

Should Objects.isNull()be confined to Predicates exclusively?

应该Objects.isNull()仅限于谓词吗?

采纳答案by Suresh Atta

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

应该在 if 语句中使用 object == null over Objects.isNull() 吗?

If you look at the source codeof IsNullmethod,

如果你看一下源代码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.

这是相同的。没有区别。所以你可以安全地使用它。

回答by Mena

Look at the source:

看源码:

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

To check for nullvalues, you can use:

要检查null值,您可以使用:

  • Objects.isNull(myObject)
  • null == myObject // avoids assigning by typo
  • myObject == null // risk of typo
  • Objects.isNull(myObject)
  • null == myObject // avoids assigning by typo
  • myObject == null // risk of typo

The fact that Objects.isNullis meant for Predicates does not prevent you from using it as above.

这一事实Objects.isNull是为Predicates不阻止你使用它上面的。

回答by Vasil

Would there be any reason/circumstance for which I should use object == null over Objects.isNull() in a if statement?

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

Yes, one reason is to keep the code simple. Within if statementobject == nullis clear and well known. It can not lead to any misbehavior if for example there is a typo.

是的,原因之一是保持代码简单。在if 语句中object == null是明确且众所周知的。例如,如果有错字,它不会导致任何不当行为。

My understanding is that Objects.isNull() would remove the chance of accidentally assigning a null value to object by omitting the second =.

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

If there is an if (object = null) {}with omitted=it will not compile or it will generate warning in case of Booleanobject! Actually there is no reason to use Objects.isNull(object)over object == nullwithin if statement. Here are the two variants side by side:

如果省略if (object = null) {}with ,则不会编译,或者在object 的情况下会生成警告!实际上没有理由在if 语句中使用over 。这是并排的两个变体:=BooleanObjects.isNull(object)object == null

if (object == null) {
}

if (Objects.isNull(object)) {
}

Should Objects.isNull() be confined to Predicates exclusively?

Objects.isNull() 应该仅限于谓词吗?

It could be said yes, it is confined to Predicates exclusively, although there is no technical hurdle to use the Objects.isNull()everywhere.

可以说是的,它仅限于谓词,尽管在任何Objects.isNull()地方使用都没有技术障碍。

From the public static boolean isNull(Object obj)method's javadoc:

public static boolean isNull(Object obj)方法的javadoc:

@apiNoteThis method exists to be used as a java.util.function.Predicate, filter(Objects::isNull)

@apiNote此方法存在用作 java.util.function.Predicate, filter(Objects::isNull)

So if you use the method as nota predicate you are actually using a more complex and cumbersome expression compared to the simple object == null.

因此,如果您将该方法用作不是谓词,则与简单的object == null.

Here is a snippet to compare the benefit of Objects.isNull(object)

这是一个比较以下好处的片段 Objects.isNull(object)

List<String> list = Arrays.asList("a", "b", null, "c", null);

// As ready-made predicate
long countNullsWithPredicate = list.stream().filter(Objects::isNull).count();

// Lambda
long countNullsWithLambda = list.stream().filter(object -> object == null).count();

// Reimplement the Objects::isNull predicate
long countNullsWithAnonymous = list.stream().filter(new Predicate<Object>() {
    @Override
    public boolean test(Object obj) {
        return obj == null;
    }
}).count();

回答by Craig Taylor

Objects.isNullis intended for use within Java 8 lambda filtering.

Objects.isNull旨在用于 Java 8 lambda 过滤。

It's much easier and clearer to write:

写起来更容易、更清晰:

.stream().filter(Objects::isNull) 

than to write:

比写:

.stream().filter(x -> x == null).  

Within an ifstatement, however, either will work. The use of == nullis probably easier to read but in the end it will boil down to a style preference.

if然而,在声明中,两者都可以。的使用== null可能更容易阅读,但最终归结为风格偏好。

回答by angry_snyder

Semantically there is no difference but for readability I prefer the following over whatever == null:

在语义上没有区别,但为了可读性,我更喜欢以下内容whatever == null

import static java.util.Objects.isNull;

// Other stuff...

if(isNull(whatever)) { 

}