Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Java Dictionary

  1. Home
  2. Java Dictionary
  3. Optional.of() / isPresent() / orElse() / orElseThrow()

Optional.of() / isPresent() / orElse() / orElseThrow()

Optional is a wrapper class for safely handling null values (Java 8+). It provides a unified way to handle both present and absent values, reducing the risk of NullPointerException. It is also commonly used as the return type of terminal stream operations such as findFirst().

Syntax

// Creates an Optional with a value (null is not allowed).
Optional<Type> opt = Optional.of(value);

// Creates an Optional that allows null.
Optional<Type> opt = Optional.ofNullable(valueOrNull);

// Creates an empty Optional.
Optional<Type> opt = Optional.empty();

// Checks whether a value is present.
opt.isPresent();

// Retrieves the value (throws an exception if absent).
opt.get();

// Specifies a default value to return when no value is present.
opt.orElse(defaultValue);

Method List

MethodDescription
Optional.of(T value)Creates an Optional with a value. Throws NullPointerException if null is passed.
Optional.ofNullable(T value)Creates an Optional that allows null. Returns an empty Optional if the value is null.
Optional.empty()Creates an empty Optional.
isPresent()Checks whether a value is present and returns a boolean.
get()Retrieves the value. Throws NoSuchElementException if no value is present.
orElse(T other)Returns the value if present, or the specified default value if absent.
map(Function)Applies a mapping function to the value if present and returns a new Optional.
filter(Predicate)Returns the Optional if the value satisfies the condition, or an empty Optional if it does not.

Sample Code

import java.util.Optional;

// Creates an Optional with a value.
Optional<String> opt = Optional.of("Hello");
System.out.println(opt.isPresent()); // Prints: true
System.out.println(opt.get());       // Prints: Hello

// Creates an Optional that allows null.
String nullValue = null;
Optional<String> optNull = Optional.ofNullable(nullValue);
System.out.println(optNull.isPresent()); // Prints: false

// Uses orElse to specify a default value.
String result = optNull.orElse("Default");
System.out.println(result); // Prints: Default

// Uses map to transform the value (only runs if a value is present).
Optional<Integer> length = opt.map(String::length);
System.out.println(length.orElse(0)); // Prints: 5

// Uses filter to narrow down by condition.
Optional<String> filtered = opt.filter(s -> s.startsWith("H"));
System.out.println(filtered.isPresent()); // Prints: true

// Uses ifPresent to run code only when a value is present.
opt.ifPresent(s -> System.out.println("Value: " + s)); // Prints: Value: Hello

Notes

Optional makes it explicit at the type level that a return value may be absent, prompting the caller to handle the null case. Using Optional for field variables or method parameters is not recommended — use it primarily as a method return type.

Java 11 added isEmpty() (the inverse of isPresent()), and Java 9 added ifPresentOrElse() (which also handles the absent case). Avoid calling opt.get() directly; instead, make a habit of using orElse() or ifPresent().

For search operations on streams, see findFirst() / anyMatch() / allMatch().

If you find any errors or copyright issues, please .