Optional get() method in Java with examples Last Updated : 30 Jul, 2019 Comments Improve Suggest changes Like Article Like Report 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: This method returns the value of this instance of the Optional class. Exception: This method throws NoSuchElementExcpetion if there is no value present in this Optional instance. Below programs illustrate get() method: Program 1: Java // Java program to demonstrate // Optional.get() method import java.util.*; public class GFG { public static void main(String[] args) { // create a Optional Optional<Integer> op = Optional.of(9455); // print value System.out.println("Optional: " + op); // get the value System.out.println("Value of this Optional: " + op.get()); } } Output: Optional: Optional[9455] Value of this Optional: 9455 Program 2: Java // Java program to demonstrate // Optional.get() method import java.util.*; public class GFG { public static void main(String[] args) { // create a Optional Optional<Integer> op = Optional.empty(); // print value System.out.println("Optional: " + op); try { // get the value System.out.println("Value of " + "this Optional: " + op.get()); } catch (Exception e) { System.out.println(e); } } } Output: Optional: Optional.empty java.util.NoSuchElementException: No value present Reference: https://docs.oracle.com/javase/9/docs/api/java/util/Optional.html#get-- Comment More infoAdvertise with us Next Article Optional get() method in Java with examples S ShubhamMaurya3 Follow Improve Article Tags : Java Java - util package Java-Functions Java-Optional Practice Tags : Java Similar Reads Optional filter() method in Java with examples The filter() method of java.util.Optional class in Java is used to filter the value of this Optional instance by matching it with the given Predicate, and then return the filtered Optional instance. If there is no value present in this Optional instance, then this method returns an empty Optional in 2 min read 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 Optional or() method in Java with examples The or() method of java.util.Optional class in Java is used to get this Optional instance if any value is present. If there is no value present in this Optional instance, then this method returns an Optional instance with the value generated from the specified supplier. Syntax: public Optional<T 2 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 Optional hashCode() method in Java with examples The hashCode() method of java.util.Optional class in Java is used to get the hashCode value of this Optional instance. If there is no value present in this Optional instance, then this method returns 0. Syntax: public int hashCode() Parameter: This method do not accepts any parameter. Return Value: 1 min read Like