Java Optional empty() Method
Example
Create an empty Optional:
import java.util.Optional;
public class Main {
public static void main(String[] args) {
Optional<String> noName = Optional.empty();
System.out.println(noName.isPresent());
System.out.println(noName.orElse("Guest"));
}
}
Definition and Usage
The empty() method returns an empty Optional (an Optional that holds no value).
Use it, for example, as the return value of a method that did not find what it was looking for.
Tip: Use of() to create an Optional that holds a value.
Syntax
public static <T> Optional<T> empty()
T refers to the data type of the value that the Optional can hold.
Parameter Values
None.
Technical Details
| Returns: | An empty Optional. |
|---|
Related Pages
Java Lambda Expressions Tutorial
❮ Optional Methods