The ofNullable() method of the Optional class creates an Optional containing the specified value when it is non-null. If the value is null, it returns an empty Optional instead.
- It is useful when a value might be null and needs to be represented using Optional.
- Unlike Optional.of(), ofNullable() handles both non-null and null values without throwing a NullPointerException.
Example: Program to demonstrate how ofNullable() creates an Optional for a non-null value.
import java.util.Optional;
public class GFG {
public static void main(String[] args) {
String value = "GeeksforGeeks";
Optional<String> result = Optional.ofNullable(value);
System.out.println(result);
}
}
Output
Optional[GeeksforGeeks]
Explanation: The value variable contains a non-null string, so Optional.ofNullable() creates an Optional containing that value. If value were null, the method would return Optional.empty() instead.
Syntax:
public static <T> Optional<T> ofNullable(T value)
The method returns an Optional containing the specified value. For a null value, it returns an empty Optional.
Working of ofNullable()
ofNullable() checks the supplied value and creates an Optional containing the value when it is non-null or returns an empty Optional when it is null.
- Provide Value: A value is passed to ofNullable().
- Check Value: The method checks whether the value is null.
- Non-Null Value: If the value is not null, an Optional containing that value is returned.
- Null Value: If the value is null, Optional.empty() is returned.
Using ofNullable() with Different Values
The ofNullable() method handles both non-null and null values by creating an Optional containing the value or returning an empty Optional, respectively.
1: Using ofNullable() with a Non-Null Value
The following program passes an integer value to ofNullable(). The value is stored inside the Optional object.
import java.util.*;
public class GFG {
public static void main(String[] args) {
Optional<Integer> op1 =
Optional.ofNullable(9455);
System.out.println("Optional 1: " + op1);
}
}
Output
Optional 1: Optional[9455]
Explanation: The value 9455 is not null, so ofNullable() creates an Optional containing that value. Printing the object displays Optional[9455].
2: Using ofNullable() with a Null Value
The following program passes null to ofNullable().
import java.util.*;
public class GFG {
public static void main(String[] args) {
Optional<String> op2 =
Optional.ofNullable(null);
System.out.println("Optional 2: " + op2);
}
}
Output
Optional 2: Optional.empty
Explanation: Since the specified value is null, ofNullable() returns an empty Optional. The empty instance is displayed as Optional.empty.
3: Using ofNullable() with a Variable
ofNullable() is particularly useful when a variable receives its value from a method, database, collection, or another source where the value might be null.
import java.util.Optional;
public class GFG {
public static void main(String[] args) {
String name = null;
Optional<String> result = Optional.ofNullable(name);
System.out.println(result);
}
}
Output
Optional.empty
Explanation: The name variable contains null, so ofNullable() returns an empty Optional instead of throwing an exception.
4: Using ofNullable() with Method Return Value
ofNullable() is also useful when a method might return null. Instead of directly working with the returned value, the result is wrapped in an Optional.
import java.util.Optional;
public class GFG {
static String getName() {
return null;
}
public static void main(String[] args) {
Optional<String> name = Optional.ofNullable(getName());
System.out.println(name);
}
}
Output
Optional.empty
Explanation: The getName() method returns null. ofNullable() converts this null result into Optional.empty(), providing an explicit representation of the absence of a value.
ofNullable() vs of()
The main difference between ofNullable() and of() is how they handle null values.
| Feature | of() | ofNullable() |
|---|---|---|
| Non-null value | Creates Optional | Creates Optional |
| Null value | Throws NullPointerException | Returns Optional.empty() |
| Null handling | Does not support null | Supports null |
| Use | Value is known to be non-null | Value might be null |