OptionalInt of(int) method in Java with examples Last Updated : 02 May, 2019 Comments Improve Suggest changes Like Article Like Report The of(int) method help us to get an OptionalInt object which contains a int value which is passed as a parameter to this method. Syntax: public static OptionalInt of(int value) Parameters: This method accepts a int value as parameter that will be set to the returned OptionalInt object. Return value: This method returns an OptionalInt with the value present. Below programs illustrate of(int) method: Program 1: Java // Java program to demonstrate // OptionalInt.of(int) method import java.util.OptionalInt; public class GFG { public static void main(String[] args) { // Create a OptionalInt instance // using of() method OptionalInt opInt = OptionalInt.of(452); // Get value of this OptionalInt instance System.out.println("OptionalInt: " + opInt); } } Output: OptionalInt: OptionalInt[452] Program 2: Java // Java program to demonstrate // OptionalInt.of(int) method import java.util.OptionalInt; public class GFG { public static void main(String[] args) { // Create a OptionalInt instance // using of() method OptionalInt opInt = OptionalInt.of(2149); // Get value of this OptionalInt instance System.out.println("OptionalInt: " + opInt); } } Output: OptionalInt: OptionalInt[2149] References: https://docs.oracle.com/javase/10/docs/api/java/util/OptionalInt.html#of(int) Comment More infoAdvertise with us Next Article OptionalInt of(int) method in Java with examples A AmanSingh2210 Follow Improve Article Tags : Java Java - util package Java-Functions Java-OptionalInt Practice Tags : Java Similar Reads Optional of() method in Java with examples The of() method of java.util.Optional class in Java is used to get an instance of this Optional class with the specified value of the specified type. Syntax: public static <T> Optional<T> of(T value) Parameters: This method accepts value as parameter of type T to create an Optional insta 1 min read OptionalInt isPresent() method in Java with examples OptionalInt help us to create an object which may or may not contain a Int value. The isPresent() method help us to get the answer that Int value is present in OptionalInt object or not. If an int value is present in this object, this method returns true, otherwise false. Syntax: public boolean isPr 1 min read OptionalInt getAsInt() method in Java with examples OptionalInt help us to create an object which may or may not contain a int value. The getAsInt() method returns value If a value is present in OptionalInt object, otherwise throws NoSuchElementException. Syntax: public int getAsInt() Parameters: This method accepts nothing. Return value: This method 2 min read OptionalInt orElse(int) method in Java with examples The orElse(int) method helps us to get the value in this OptionalInt object. If a value is not present in this OptionalInt, then this method returns the value passed as the parameter. Syntax: public int orElse(int other) Parameters: This method accepts int the value to be returned, if no value is pr 1 min read Optional get() method in Java with examples The get() method of java.util.Optional class in Java is used to get the value of this Optional instance. If there is no value present in this Optional instance, then this method throws NullPointerException. Syntax: public T get() Parameters: This method do not accept any parameter. Return value: Thi 2 min read Like