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()); } } |
chevron_right
filter_none
Output : true Output : false
Recommended Posts:
- Optional isPresent() method in Java with examples
- Java 8 Optional Class
- Optional Class | Guava | Java
- How to avoid NullPointerException in Java using Optional class?
- OptionalDouble isPresent() method in Java with examples
- OptionalLong isPresent() method in Java with examples
- OptionalInt isPresent() method in Java with examples
- Optional of() method in Java with examples
- Optional get() method in Java with examples
- Optional or() method in Java with examples
- Optional toString() method in Java with examples
- Optional filter() method in Java with examples
- Optional ofNullable() method in Java with examples
- Optional stream() method in Java with examples
- Optional empty() method in Java with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



