Fantastic Java API and Where to Find Them: Objects.isNull and Objects.nonNull

Massimo Caliman
Jan 28, 2020


Photo by Kenny Eliason on Unsplash

Once upon a time a NullPointerException

There are two simple, elegant, clean methods to manage null with Predicate: Object.isNull and Object.nonNull.

It’s much easier and clearer to write: stream.filter(Objects::isNull)

than to write: stream.filter(x -> x == null)

However, the fact that Objects.isNull is meant for Predicates does not prevent you from using it as above…

Your code might be more readable in this fashion.

if(isNull(aValue)) doSomething();
if(nonNull(aValue)) doSomething();

Think about it.

Simple, clean, and you don’t test it. Oracle has already tested it for you.