Optional isPresent() Method in Java Last Updated : 24 Apr, 2025 Comments Improve Suggest changes Like Article Like Report The isPresent() method of the java.util.Optional class is used to check if a value is present in the Optional object. If there is no value present in this Optional instance, then this method returns false, else true.Syntax of Optional isPresent() Methodpublic boolean isPresent()Parameters: This method does not accept any parameters.Return value: This method returns a Boolean value stating whether there is a value present in this Optional instance.Now let us see some examples of the Optional isPresent() method.Example 1: Using Optional.of()This example finds out that a value is present inside a non-empty Optional. Java // Java program to demonstrate Optional.isPresent() // with a non-empty Optional import java.util.Optional; public class Geeks { public static void main(String[] args) { // Create an Optional with a non-null value Optional<Integer> optional = Optional.of(9455); // Print the Optional value System.out.println("Optional: " + optional); // Check if a value is present System.out.println("Is any value present: " + optional.isPresent()); } } OutputOptional: Optional[9455] Is any value present: true Example 2: Using Optional.empty()This example shows how isPresent() returns false for an empty Optional. Java // Java program to demonstrate Optional.isPresent() // with an empty Optional import java.util.Optional; public class Geeks { public static void main(String[] args) { // Create an empty Optional Optional<Integer> optional = Optional.empty(); // Print the Optional value System.out.println("Optional: " + optional); // Check if a value is present System.out.println("Is any value present: " + optional.isPresent()); } } OutputOptional: Optional.empty Is any value present: false Note: The isPresent() method is still valid, but it is not the preferred approach for Java 11+ development. Consider using alternatives like ifPresent(), orElse(). Comment More infoAdvertise with us Next Article Optional isPresent() Method in Java S ShubhamMaurya3 Follow Improve Article Tags : Java Java - util package Java-Functions Java-Optional Practice Tags : Java Similar Reads 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 OptionalLong isPresent() method in Java with examples OptionalLong help us to create an object which may or may not contain a Long value. The isPresent() method help us to get the answer that a value is present in OptionalLong object or not. If a long value is present in this object, this method returns true, otherwise false. Syntax: public boolean isP 1 min read OptionalDouble isPresent() method in Java with examples OptionalDouble help us to create an object which may or may not contain a Double value. The isPresent() method help us to get the answer that double value is present in OptionalDouble object or not. If a double value is present, returns true, otherwise false. Syntax: public boolean isPresent() Param 1 min read Vector isEmpty() Method in Java The Java.util.Vector.isEmpty() method in Java is used to check and verify if a Vector is empty or not. It returns True if the Vector is empty else it returns False. Syntax: Vector.isEmpty() Parameters: This method does not take any parameter. Return Value: This function returns True if the Vectoris 2 min read Optional ifPresentOrElse() method in Java with examples The ifPresentOrElse(Consumer, Runnable) method of java.util.Optional class helps us to perform the specified Consumer action the value of this Optional object. If a value is not present in this Optional, then this method performs the given empty-based Runnable emptyAction, passed as the second param 2 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 Optional orElseGet() Method in Java The orElseGet() method of java.util.Optional class in Java is used to get the value of this Optional instance if present. If there is no value present in this Optional instance, then this method returns the value generated from the specified supplier.Syntax of orElseGet() Methodpublic T orElseGet(Su 2 min read Optional empty() method in Java with examples The empty() method of java.util.Optional class in Java is used to get an empty instance of this Optional class. This instance do not contain any value. Syntax: public static <T> Optional<T> empty() Parameters: This method accepts nothing. Return value: This method returns an empty instan 1 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 Like