Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should Optional.ofNullable() be used for null check?

Which null-check is preferable?

Optional.ofNullable(port).ifPresent(settings::setPort);

or

if (port != null) {
   settings.setPort(port);
}
like image 607
applejuice Avatar asked Aug 27 '18 11:08

applejuice


People also ask

What happens if the specified value is null in an optional?

If the specified value is null, then this method returns an empty instance of the Optional class. Parameters: This method accepts value as parameter of type T to create an Optional instance with this value.

What is optional ofnullable() method in Java 8 API?

A quick guide to Optional ofNullable () method in Java 8 API with Example programs. How to use in the real time projects. 1. Introduction In this tutorial, We'll learn Optional ofNullable () example on how to create new Java 8 Optional object for any value or null value. Optional is part of java.util package.

How does optional take away the need for null checks?

Let's see how Optional takes away the need for null checks: By returning an Optional, as shown above, the process method makes it clear to the caller that the response can be empty and needs to be handled at compile time. This notably takes away the need for any null checks in the client code.

What is the difference between ofnullable() and optional empty() methods?

But, seeing Optional.empty for usOptional. Because ofNullable method internally calls Optional.empty () method if the null value is passed to it. See the internal logic to understand how it is implemented. This method uses two methods. If the value is null, it calls empty () method. If the value is non-null, it calls of () method.


2 Answers

In Java, an Optional value is a fusion of a bit that indicates presence or absence, with a value of an arbitrary reference type T or a primitive int, long, or double.

Fusing these is especially useful when returning a value from a method, as methods have only a single return value. It's often necessary to use a special value such as null in the case of reference types, or -1 in the case of int, as a sentinel to indicate the "no-value" case. Using Optional as a return value avoids the problem of the caller accidentally misusing the sentinel value as the real return value.

Given this, line of code such as

Optional.ofNullable(port).ifPresent(settings::setPort);

is strange in that it fuses a value with the present/absent bit in the first part of the line and then immediately separates them in the second part of the line. This adds complexity to what is ultimately a fairly simple task: checking whether port is non-null and conditionally performing some action. The alternative code snippet:

if (port != null) {
    settings.setPort(port);
}

expresses quite clearly exactly what it does.

It's true that the if-statement takes more vertical space than the Optional chain. The Optional chain is denser, but it's also harder to understand: a poor tradeoff.

like image 63
Stuart Marks Avatar answered Oct 02 '22 12:10

Stuart Marks


It is dependent on several factors when to use this. If port is a property within the class, probably using Optional is a bit overkill. (and don't useOptionals as property as they are not serializbie)

I think Optionals are great when for example writing a library.

public Optional<Integer> getPort()

Is much more descriptive for other developers then

// Returns null if not set
public Integer getPort()
like image 34
Albert Bos Avatar answered Oct 02 '22 12:10

Albert Bos