Java 8 Optional Class

Last Updated : 16 May, 2026

Java 8 introduced the Optional class to handle null values safely. It is a container object that may or may not contain a value. It helps avoid NullPointerException and improves code readability.

  • Used to handle null values safely.
  • Reduces chances of NullPointerException.
  • Provides methods to check and process values easily.

Why Do We Need Optional

Traditionally, developers relied on null to represent the absence of a value, which often caused NullPointerException.

Example: Java program without the Optional Class

Java
public class OptionalDemo {
    public static void main(String[] args)
    {
        String[] words = new String[10];
        String word = words[5].toLowerCase();
        System.out.print(word);
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException

To avoid abnormal termination, we use the Optional class. In the following example, we are using Optional. So, our program can execute without crashing.

Example: Program using Optional Class

Java
import java.util.Optional;

// Driver Class
public class OptionalDemo {
      // Main Method
    public static void main(String[] args)
    {
        String[] words = new String[10];
        
          Optional<String> checkNull = Optional.ofNullable(words[5]);
        
          if (checkNull.isPresent()) {
            String word = words[5].toLowerCase();
            System.out.print(word);
        }
        else
            System.out.println("word is null");
    }
}

Output
word is null

Creating Optional Objects

  • Optional.empty(): Returns an empty Optional.
  • Optional.of(value): Returns an Optional containing the given non-null value.

Example 1: Using empty() and of()

Java
import java.util.Optional;

class GFG {
    public static void main(String[] args) {
        String[] str = new String[5];
        str[2] = "Geeks Classes are coming soon";

        Optional<String> empty = Optional.empty();
        System.out.println(empty); 

        Optional<String> value = Optional.of(str[2]);
        System.out.println(value); 
    }
}

Output
Optional.empty
Optional[Geeks Classes are coming soon]

Explanation: In this example, Optional.empty() creates an Optional object with no value. Then, Optional.of(str[2]) creates an Optional that contains a non-null string. The print statements display an empty Optional and an Optional holding a value, showing how Optional safely represents both cases.

Example 2: Using get(), hashCode() and isPresent()

Java
import java.util.Optional;

class GFG {
    public static void main(String[] args) {
        String[] str = new String[5];
        str[2] = "Geeks Classes are coming soon";

        Optional<String> value = Optional.of(str[2]);

        System.out.println(value.get());       
        System.out.println(value.hashCode());  
        System.out.println(value.isPresent()); 
    }
}

Output
Geeks Classes are coming soon
1967487235
true

Explanation: Here, Optional.of(str[2]) stores a value inside the Optional object. The get() method retrieves that value, hashCode() returns its hash value, and isPresent() checks whether a value exists. Since the value is present, it returns true.

Common Methods in Optional

The Optional class offers methods such as isPresent() orElse() and ifPresent() to handle potentially null values more safely and functionally. The table below lists commonly used Optional methods and their descriptions.

Method

Description

empty()

Returns an empty Optional instance

of(T value)

Returns an Optional with the specified present non-null value.

ofNullable(T value)

Returns an Optional describing the specified value if non-null, else empty

equals(Object obj)

Checks if another object is "equal to" this Optional.

filter(Predicate<? super T> predicate)

If a value is present and matches the predicate, returns an Optional with the value; else returns empty.

flatMap(Function<? super T, Optional<U>> mapper)

If a value is present, applies a mapping function that returns an Optional; otherwise returns empty.

get()

Returns the value if present, else throws NoSuchElementException.

hashCode()

Returns the hash code of the value if present, otherwise returns 0.

ifPresent(Consumer<? super T> consumer)

Returns true if a value is present, else false.

map(Function<? super T, ? extends U> mapper)

If a value is present, applies the mapping function and returns an Optional describing the result (if non-null).

orElse(T other)

Returns the value if present, otherwise returns the provided default value.

orElseGet(Supplier<? extends T> other)

Returns the value if present, otherwise invokes the supplier and returns its result.

orElseThrow(Supplier<? extends X> exceptionSupplier)

Returns the value if present, otherwise throws an exception provided by the supplier

toString()

Returns a non-empty string representation of this Optional for debugging.

Comment