OptionalInt getAsInt() method in Java with examples Last Updated : 30 Jul, 2019 Comments Improve Suggest changes Like Article Like Report 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 returns the value described by this OptionalInt. Exception: This method throws NoSuchElementException if no value is present Below programs illustrate getAsInt() method: Program 1: Java // Java program to demonstrate // OptionalInt.getAsInt() method import java.util.OptionalInt; public class GFG { public static void main(String[] args) { // Create an OptionalInt instance OptionalInt opInt = OptionalInt.of(45); System.out.println("OptionalInt: " + opInt.toString()); // Get value in this instance // using getAsInt() System.out.println("Value in OptionalInt = " + opInt.getAsInt()); } } Output: OptionalInt: OptionalInt[45] Value in OptionalInt = 45 Program 2: Java // Java program to demonstrate // OptionalInt.getAsInt() method import java.util.OptionalInt; public class GFG { public static void main(String[] args) { try { // Create an OptionalInt instance OptionalInt opInt = OptionalInt.empty(); System.out.println("OptionalInt: " + opInt.toString()); // Get value in this instance // using getAsInt() System.out.println("Value in OptionalInt = " + opInt.getAsInt()); } catch (Exception e) { System.out.println("Exception: " + e); } } } Output: OptionalInt: OptionalInt.empty Exception: java.util.NoSuchElementException: No value present References: https://docs.oracle.com/javase/10/docs/api/java/util/OptionalInt.html#getAsInt() Comment More infoAdvertise with us Next Article OptionalInt getAsInt() 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 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 OptionalInt of(int) method in Java with examples 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 1 min read OptionalLong getAsLong() method in Java with examples OptionalLong help us to create an object which may or may not contain a long value. The getAsLong() method returns value If a value is present in OptionalLong object, otherwise throws NoSuchElementException. Syntax: public long getAsLong() Parameters: This method accepts nothing. Return value: This 2 min read OptionalInt hashCode() method in Java with examples The hashCode() method help us to get the hash code of the value, if Int value is present, otherwise 0 (zero) if no Int value is present in OptionalInt object. Syntax: public int hashCode() Parameters: This method accepts does not accepts any parameter. Return value: This method returns hash code val 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 Like