java Objects.isNull(...) / Objects.nonNull(...) 的用途
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28140193/
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
Purpose of Objects.isNull(...) / Objects.nonNull(...)
提问by Ivan Babanin
What is the purpose of
目的是什么
Objects.isNull(x)
if we can simply write
如果我们可以简单地写
x == null
?
?
Same for
同为
Objects.nonNull(...)
and
和
x != null
回答by Petr Jane?ek
From the JavaDoc of the method:
API Note: This method exists to be used as a
Predicate
,filter(Objects::isNull)
API 注意:此方法存在用作
Predicate
,filter(Objects::isNull)
回答by rai.skumar
Apart from its obvious usage in the functional world. It can also be used in your normal code in place of ==
.
除了它在功能世界中的明显用法。它也可以在您的普通代码中代替==
.
Many programmers (including me) think that ( x == null
or x != null
) are not object-orientedand hence it makes sense to use object-oriented version.
许多程序员(包括我)认为(x == null
或x != null
) 不是面向对象的,因此使用面向对象的版本是有意义的。
Objects Java doc:
对象 Java 文档:
This class consists of static utility methods for operating on objects. These utilities include null-safe or null-tolerant methods for computing the hash code of an object, returning a string for an object, and comparing two objects.
此类包含用于操作对象的静态实用程序方法。这些实用程序包括用于计算对象的哈希码、返回对象的字符串以及比较两个对象的空安全或空值容忍方法。
I personally prefer the method version and have been using it for the last couple of years :)
我个人更喜欢方法版本,并且过去几年一直在使用它:)