Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
One of such methods is
Objects.isNull()
.
My understanding is that
Objects.isNull()
would remove the chance of accidentally assigning a null value to object by omitting the second
=
.
However, the API Note states:
This method exists to be used as a Predicate, filter(Objects::isNull)
Would there be any reason/circumstance for which I should use
object == null
over
Objects.isNull()
in an
if statement
?
Should
Objects.isNull()
be confined to Predicates exclusively?
–
–
–
–
Objects.isNull
is intended for use within Java 8 lambda filtering.
It's much easier and clearer to write:
.stream().filter(Objects::isNull)
than to write:
.stream().filter(x -> x == null).
Within an if
statement, however, either will work. The use of == null
is probably easier to read but in the end it will boil down to a style preference.
–
–
–
If you look at the source code of IsNull
method,
/* 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.
–
–
–
Objects.isNull(myObject)
null == myObject // avoids assigning by typo
myObject == null // risk of typo
The fact that Objects.isNull
is meant for Predicate
s does not prevent you from using it as above.
–
–
–
–
Would there be any reason/circumstance for which I should use object == null over Objects.isNull() in a if statement?
Yes, one reason is to keep the code simple. Within if statement object == null
is clear and well known. It can not lead to any misbehavior if for example there is a typo.
My understanding is that Objects.isNull() would remove the chance of accidentally assigning a null value to object by omitting the second =.
If there is an if (object = null) {}
with omitted =
it will not compile or it will generate warning in case of Boolean
object! Actually there is no reason to use Objects.isNull(object)
over object == null
within if statement. Here are the two variants side by side:
if (object == null) {
if (Objects.isNull(object)) {
Should Objects.isNull() be confined to Predicates exclusively?
It could be said yes, it is confined to Predicates exclusively, although there is no technical hurdle to use the Objects.isNull()
everywhere.
From the public static boolean isNull(Object obj)
method's javadoc:
@apiNoteThis method exists to be used as a java.util.function.Predicate, filter(Objects::isNull)
So if you use the method as not a predicate you are actually using a more complex and cumbersome expression compared to the simple object == null
.
Here is a snippet to compare the benefit of 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();
Semantically there is no difference but for readability I prefer the following over whatever == null
:
import static java.util.Objects.isNull;
// Other stuff...
if(isNull(whatever)) {
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.