In Java, the OptionalDouble
object is a container object that may or may not contain a double
value.
The
OptionalDouble
class is present in thejava.util
package.
of
methodThe of
method is used to get an instance of the OptionalDouble
class with the specified double
value.
public static OptionalDouble of (double value)
This method takes as a parameter the double
value that is to be present in the OptionalDouble
object.
This method returns an OptionalDouble
object with the specified double
value.
The code below shows how to use the of
method.
import java.util.OptionalDouble;class OptionalDoubleOfExample {public static void main(String[] args) {OptionalDouble optional1 = OptionalDouble.of(1.5);System.out.println("Optional 1: " + optional1);OptionalDouble optional2 = OptionalDouble.of(10.8967);System.out.println("Optional 2: " + optional2);}}
In the code above:
OptionalDouble
class.import java.util.OptionalDouble;
of
method to get an OptionalDouble
object with the double
value 1.5
.OptionalDouble optional1 = OptionalDouble.of(1.5);
optional1; // OptionalDouble[1.5]
of
method to get an OptionalDouble
object with the value 10.8967
.OptionalDouble optional2 = OptionalDouble.of(10.8967);
optional2;// OptionalDouble[10.8967]