Java 8 Optional. Why of and ofNullable?

hades

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?

Naman

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java 8 Optional. Why of and ofNullable?

From Dev

Optional ofNullable execution flow in java 8

From Dev

How does this work - Java 8 Optional.of and Optional.ofNullable

From Dev

Java Optional.ofNullable with Mockito

From Java

Why use Optional.of over Optional.ofNullable?

From Dev

Why have multiple version of Optional in Java 8

From Dev

How to use `Optional.ofNullable(variable)` in java 7 version

From Dev

Optional.ofNullable and method chaining

From Java

Why should Java 8's Optional not be used in arguments

From Dev

Why is Java 8 Optional implemented as final, without Some and None hierarchy?

From Java

Logging in Java 8 Optional

From Dev

Java 8 Optional asSet()

From Dev

Java 8 Optional usage

From Dev

Java 8 Optional instead of if

From Dev

Java 8 Optional usage

From Dev

Guava Optional to Java 8 Optional with multiple choices

From Dev

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

From Dev

Null pointer exception inside Optional.ofNullable()

From Dev

Optional.ofNullable - Cannot return a void result

From Dev

Possible redundant object creation with Optional#ofNullable?

From Dev

Java 8 Optional get if present

From Java

Use Java 8 optional with Mapstruct

From Dev

Using Java 8 Optional with JSONObject?

From Dev

Java 8 - Stream, filter and optional

From Dev

Subclasses with Java 8 lambdas and Optional

From Dev

The signature of flatMap in Optional of Java 8

From Dev

If - Else using Optional - Java 8

From Dev

Using Java 8 Optional with JSONObject?

From Dev

Java 8 Optional<T> get