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.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Massimo Caliman
Massimo Caliman

Written by Massimo Caliman

Software Architect, Developer, Java enthusiast

No responses yet

Write a response