Explain Codes LogoExplain Codes Logo

Java.util.objects.isnull vs object == null

java
null-safety
functional-programming
best-practices
Alex KataevbyAlex Kataev·Feb 27, 2025
TLDR

For null checks, the conventional direct comparison tying Java's heart since its inception is object == null. If you prefer something a bit more suave with a subtle taste of functional programming, Objects.isNull(object) is your oyster, especially when dealing with modern Java features like streams and method references:

boolean isNull = Objects.isNull(someObject); // Spreading nullity in style!

Whether you dance with object == null in the ballroom of simplicity or charm Objects.isNull at the mysterious lounge of functional constructs, the ultimate goal remains the same – facilitating readability.

Object comparisons: The classic way

The classic way of finding if the princess is in another castle (read: object == null) is universally acknowledged by all Java practitioners. It’s simple and doesn’t beat around the bush:

if (object == null) { // Handle null case // Looks like the princess is in another castle. }

However, some codebases may prefer the modern charm of Objects::isNull especially when dealing with optional values or aligning with a more functional style of Java:

Optional.ofNullable(object).filter(Objects::isNull).ifPresent(o -> { // React to null value // Princess located in this castle. });

Remember, sticking to one style boosts maintainability and prevents your codebase from looking like Frankenstein's creation!

Null checks in streams: Functional elegance

Little did we know that our trusty method reference Objects::isNull can weave magic with stream operations. Imagine a squad of rebellious nulls trying to sneak into your collection:

List<String> items = Arrays.asList("a", null, "c"); List<String> nonNullItems = items.stream() .filter(Objects::nonNull) .collect(Collectors.toList()); // Null army, thou shalt not pass!

Reversing the charm to Objects::isNull we can effortlessly weed out the nulls, preserving the tranquility of our collection, oozing with compact and functional style.

Avoiding the booby trap of accidental assignments

Channeling Objects.isNull() can save you from a fatal typo. Imagine, an innocent typo of if (object = null), cat food for the interpreter, but logically faulty. Clutching Objects.isNull() like a shield, shields you:

if (Objects.isNull(object)) { // No accidental assignment possible. // No nulls were hurt in the making of this code. }

No compromise on performance

Hold on! What about performance? Fear not, the difference between object == null and Objects.isNull() doesn't hold the power to dictate your performance stats. They are as identical as two peas in a pod.

The mystery behind Objects.isNull

Sharpening your curiosity, you delve into the inner workings of Objects.isNull(), to find a simple nullity check:

public static boolean isNull(Object obj) { return obj == null; // I told you we're buddies! }

Shattering the illusion of superiority, this evidence champions readability and stylistic choice as the deciders of the battle between 'object == null' and 'Objects.isNull()'.