A NullPointerException is the most common runtime crash in Java. It happens when you call a method on a variable that holds null. The Optional class gives you a way to represent "this value might be absent" in your type system.
Instead of returning null:
Optional<String> findUser(int id) {
if (id == 1) return Optional.of("Alice");
return Optional.empty();
}
The caller is forced to handle the empty case:
Optional<String> user = findUser(42);
user.ifPresent(name -> System.out.println(name));
String name = user.orElse("Unknown");
Without Optional, you'd forget a null check somewhere and get a crash at runtime. With Optional, the compiler reminds you that the value might not be there.