The of
method gets an instance of the Optional
class with the specified integer value.
In Java, the
OptionalInt
object is a container object which may or may not contain aninteger
value. TheOptionalInt
class is present in thejava.util
package.
public static OptionalInt of(int value)
The int
value to be present in the OptionalInt
object.
This method returns an OptionalInt
object with the specified integer value.
The code below denotes how the of
method is used:
import java.util.OptionalInt;class OptionalIntOfExample {public static void main(String[] args) {OptionalInt optional1 = OptionalInt.of(1);System.out.println("Optional 1: " + optional1);OptionalInt optional2 = OptionalInt.of(100);System.out.println("Optional 2: " + optional2);}}
OptionalInt
class.import java.util.OptionalInt;
of
method to get an OptionalInt
object with the integer whose value is 1.OptionalInt optional1 = OptionalInt.of(1);
optional1; // OptionalInt[1]
of
method to get an OptionalInt
object whose value is 100.OptionalInt optional2 = OptionalInt.of(100);
optional2;// OptionalInt[100]