Class OptionalExamples

java.lang.Object
net.jrodolfo.java_evolution.java08.OptionalExamples

public class OptionalExamples extends Object
Demonstrates Optional, introduced in Java 8 as a container for values that may be absent.

Before Java 8, many methods returned null when a value could not be found. That made absence invisible in the method signature, so callers had to remember defensive checks and failures often appeared later as NullPointerExceptions.

Optional solves part of that problem by making absence explicit in a return type. A method returning Optional<String> communicates that a value may be missing and encourages the caller to handle that case with operations such as map, filter, orElseGet, and orElseThrow. It is most useful as a return type, not as a replacement for every nullable field or parameter.