What is the OptionalDouble.empty method in Java?
In Java, the OptionalDouble object is a container object that may or may not contain a double value.
The
OptionalDoubleclass is present in thejava.utilpackage.
The empty method is used to get the empty instance of the OptionalDouble class. The returned object doesn’t have any value.
Syntax
public static OptionalDouble empty()
Argument
This method doesn’t take any arguments.
Return value
This method returns an OptionalDouble object with an empty value.
Code
The code below denotes how to use the empty method.
import java.util.OptionalDouble;class OptionalDoubleEmptyExample {public static void main(String[] args) {OptionalDouble optional = OptionalDouble.empty();// print valueSystem.out.println("OptionalDouble : " + optional);System.out.println("Has value: " + optional.isPresent());}}
Explanation
In the code above:
-
In line 1, we imported the
OptionalDoubleclass. -
In line 5, we used the
emptymethod to get an emptyOptionalDoubleobject. The returned object doesn’t have any value. -
In line 7, we printed the created
OptionalIntobject. -
In line 8, we used the
isPresentmethod to check if the object contains a value. We will getfalseas a result.