Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should one use Objects.requireNonNull()?

I have noted that many Java 8 methods in Oracle JDK use Objects.requireNonNull(), which internally throws NullPointerException if the given object (argument) is null.

public static <T> T requireNonNull(T obj) {     if (obj == null)         throw new NullPointerException();     return obj; } 

But NullPointerException will be thrown anyway if a null object is dereferenced. So, why should one do this extra null check and throw NullPointerException?

One obvious answer (or benefit) is that it makes code more readable and I agree. I'm keen to know any other reasons for using Objects.requireNonNull() in the beginning of the method.

like image 542
user4686046 Avatar asked Aug 11 '17 10:08

user4686046


People also ask

What's the point of Objects requireNonNull?

requireNonNull. Checks that the specified object reference is not null and throws a customized NullPointerException if it is. Unlike the method requireNonNull(Object, String) , this method allows creation of the message to be deferred until after the null check is made.

How do you use Objects nonNull?

The nonNull method is a static method of the Objects class in Java that checks whether the input object reference supplied to it is non-null or not. If the passed object is non-null, then the method returns true. If the passed object is null , then the method returns false.


2 Answers

Because you can make things explicit by doing so. Like:

public class Foo {   private final Bar bar;    public Foo(Bar bar) {     Objects.requireNonNull(bar, "bar must not be null");     this.bar = bar;   } 

Or shorter:

  this.bar = Objects.requireNonNull(bar, "bar must not be null"); 

Now you know:

  • when a Foo object was successfully created using new()
  • then its bar field is guaranteed be non-null.

Compare that to: you create a Foo object today, and tomorrow you invoke a method that uses that field and throws. Most likely, you will not know tomorrow why that reference was null yesterday when it got passed to the constructor!

In other words: by explicitly using this method to check incoming references you can control the point in time when the exception will be thrown. And most of the time, you want to fail as fast as possible!

The major advantages are:

  • as said, controlled behavior
  • easier debugging - because you throw up in the context of the object creation. At a point in time where you have a certain chance that your logs/traces tell you what went wrong!
  • and as shown above: the true power of this idea unfolds in conjunction with final fields. Because now any other code in your class can safely assume that bar isn't null - and thus you do not need any if (bar == null) checks in other places!
like image 119
GhostCat Avatar answered Sep 28 '22 07:09

GhostCat


Fail-fast

The code should crash as soon as possible. It should not do half of the work, dereference the null and only then crash, leaving half of some work done causing the system to be in an invalid state.

This is commonly called "fail early" or "fail-fast".

like image 21
luk2302 Avatar answered Sep 28 '22 07:09

luk2302