Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 Optional. Why of and ofNullable?

Tags:

I have a question regarding Java 8's Optional, the purpose of which is to tackle NullPointerException exceptions.

The question is, what is the reason for having both types to let us choose:

Optional.of(T value)     <-----non-null value, null value will throw NPE Optional.ofNullable(T value)   <----- nullable value 

Because what I expect is, when I use:

Optional.of(nullValue); 

It won't throw a NullPointerException.


Expanded my question after some replies:

Why would people opt for Optional instead of normal if-else for null checking?

like image 765
hades Avatar asked Dec 17 '18 06:12

hades


People also ask

What is the difference between OF and ofNullable method on optional?

So instead of returning null you can wrap it with an optional object by making of Optional. ofNullable() . And when you have a method that returns Optional with several conditional statements, and there's a case when you are certain that the produced value is non-null, then you should use Optional. of() .

When should I use optional ofNullable?

The ofNullable() method is used to get an instance of the Optional class with a specified value. If the value is null , then an empty Optional object is returned.

Why do we need optional in Java 8?

So, to overcome this, Java 8 has introduced a new class Optional in java. util package. It can help in writing a neat code without using too many null checks. By using Optional, we can specify alternate values to return or alternate code to run.

What does Optional ofNullable return?

ofNullable. Returns an Optional describing the specified value, if non-null, otherwise returns an empty Optional .


2 Answers

The javadoc of Optional.of reads that explicitly :

@throws NullPointerException if value is null 

and that is where the requirement of handling the cases as expected by you comes into picture with the use of Optional.ofNullable which is a small block of code as :

public static <T> Optional<T> ofNullable(T value) {     return value == null ? empty() : of(value); // 'Optional.of' } 

That said, the decision of choosing one over the other would still reside with the application design as if your value could possibly be null or not.


On your expectation part, that was not what the Optional was actually intended for. The API note clarifies this further (formatting mine):

Optional is primarily intended for use as a method return type where there is a clear need to represent "no result," and where using null is likely to cause error. A variable whose type is Optional should never itself be null; it should always point to an Optional instance.


purpose of Optional is to tackle NullPointerException exception.

Aside: Just to call it out clearly, that the choice would of course implicitly let you define if an NPE should be thrown at runtime or not. It's not determined at the compile time though.

like image 66
Naman Avatar answered Oct 12 '22 14:10

Naman


the purpose of Optional is to tackle NullPointerException exception

Yes, it is, but at usage time not at creation.

So when you receive an Optional from a method then you can avoid NPE by using Optional.ifPresent, Optional.orElse,Optional.orElseGet and Optional.orElseThrow methods.

But this is not the case when you're creating an Optional. Since it's your own method you have to know whether the object is nullable or not.


The main point of Optional is to provide a means for a function returning a value to indicate the absence of a return value. See this discussion. This allows the caller to continue a chain of fluent method calls.

Stuart Marks

Please read this post for more detailed explanation.

like image 36
ETO Avatar answered Oct 12 '22 13:10

ETO