Skip to content
Related Articles

Related Articles

Improve Article

Optional Class | isPresent() function

  • Difficulty Level : Easy
  • Last Updated : 22 Feb, 2018
Geek Week

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


It returns true if value is assigned otherwise false 


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

Input : Optional value2 = Optional.ofNullable(null);
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:" +
    System.out.println("Second parameter is present:" +
Output : true
Output : false

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :