Which null-check is preferable?
Optional.ofNullable(port).ifPresent(settings::setPort);
or
if (port != null) {
settings.setPort(port);
}
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.
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.
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.
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.
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.
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 useOptional
s as property as they are not serializbie)
I think Optional
s 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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With