The Java Optional class keeps at most one value and is designed to handle situations involving null values. In the Optional.ofNullable method, an instance of the class is returned with the specified value. If a null value is specified, an empty optional object will be retrieved.
This write-up will discuss in detail how to use the Optional.ofNullable method in Java.
How to Use Optional.ofNullable() Method in Java?
Optional.ofNullable() Method in Java is used in code where the value can be null or not null. The main advantage of using this specific method of Java is that it will not throw any NullPointerException at runtime. This will eventually help in creating clean and maintainable code for building applications.
Syntax
The syntax for implementing the Optional.ofNullable() Method in Java is shown below:
public static <T> Optional<T> ofNullable(T value)
Here, ofNullable is a static method and is responsible for returning optional objects.
Example 1: Using Integer Value
The code below depicts the implementation of Optional.ofNullable() Method in Java.
import java.util.*;
public class optionalExample {
public static void main(String[] args)
{//The first option contains an integer.
Optional<Integer> option1 = Optional.ofNullable(1234);
System.out.println("Optional 1: "+ option1);//A null value passed to check the output.
Optional<Integer> option2= Optional.ofNullable(null);
System.out.println("Optional 2: "+ option2);
}
}
For this specific method, we need to import the library of Java which is java.util. The code is executed so that an integer value is passed and then a null value is passed.
Output
The output returns the value for Optional 1 and for the null value, an empty instance appears on the screen.
Example 2: Using Strings to Implement Optional.ofNullable Method
Let’s pass the string values to the stated function and see how it works:
import java.util.*;
public class optionalExample {
public static void main(String[] args)
{
//String Example to implement the stated function Optional<String> option1= Optional.ofNullable("String Example");
System.out.println("Optional 1: "+ option1);//Null value to view the output
Optional<String> option2= Optional.ofNullable(null);
System.out.println("Optional 2: "+ option2);
}
}
Output
Example 3: Passing Optional Value
In this piece of code, an optional value has been passed to determine the output.
//Importing the packageimport java.util.*;
public class OptionalOfNullableExample {
public static void main(String[] args) {
Optional<String> softwareOptional = Optional.ofNullable(getValue("Software"));
System.out.println("softwareOptional : " + softwareOptional);
Optional<String> hardwareOptional = Optional.ofNullable(getValue("Hardware"));
System.out.println("hardwareOptional : " + hardwareOptional);//printing the optional values
System.out.println(softwareOptional.isPresent());
System.out.println(hardwareOptional.isPresent());
}
private static String getValue(String key) {//Declare the values to witness the resultant output
if ("Software".equals(key)) {
return "ISE Design Suite";//Since the database is not present therefore, it will pass an optional value.
} else if ("Database".equals(key)) {
return "Sql";
}
return null;
}
}
In the above code:
- There are two values declared in software and hardware optional.
- The output returns the SoftwareOptional as Software since the string passed in .equals(key) matches.
Output
In the above output:
- The software optional is returned as the value that has been entered.
- The hardware optional has returned an empty value since the argument is null.
- The internal class of Optional.empty appears if the null value has been passed.
The code examples above have described in detail the implementation of Optional.ofNullable() Method in Java.
Conclusion
The Optional.ofNullable() Method in Java is used for the implementation of both null and not null values. The optional.empty() method appears for the null values. This article has demonstrated the implementation of Optional.ofNullable() Method on both the integers and the strings.