Thursday, October 16, 2025
HomeLanguagesJavaOptional Class | isPresent() function

Optional Class | isPresent() function

The isPresent() function in Optional Class is used to evaluate whether the value if assigned to variable is present or not.

Syntax

value.isPresent()
Returns:
It returns true if value is assigned otherwise false 

Examples:

Input : Optional value1 = Optional.ofNullable(10);
        value1.isPresent()
Output : True

Input : Optional value2 = Optional.ofNullable(null);
        value2.isPresent()
Output : False




// Write Java code here
import java.util.Optional;
public class present {
public static void main(String args[]) {
  
    // Optional.ofNullable - allows passed
    // parameter to be not null.
    Optional<Integer> value1 = Optional.ofNullable(10);
  
    // Optional.ofNullable - allows passed
    // parameter to be null.
    Optional<Integer> value2 = Optional.ofNullable(null);
    System.out.println("First parameter is present:" +
                        value1.isPresent());
    System.out.println("Second parameter is present:" +
                        value2.isPresent());
}
}


Output : true
Output : false
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS