Optional Class
Explore how the Java 8 Optional class improves code safety by avoiding null pointer exceptions. Understand its key methods such as of, empty, isPresent, orElse, and functional style operations like filter and map to handle optional values effectively.
We'll cover the following...
We'll cover the following...
Introduction
Java 8 comes with the Optional class in the java.util package for avoiding null return values (and thus NullPointerException). It is very similar to Google Guava’s Optional, which is similar to Nat Pryce’s Maybe class and Scala’s Option class.
Billion Dollar Mistake: Tony Hoare, the inventor of
null, has gone on record calling it his “billion-dollar mistake.” Despite your opinion ofnull, many efforts have been made to make null-checks part ...