Getting ready
The Optional class is a wrapper around a value, which can be null or a value of any type. It was intended to help to avoid the dreaded NullPointerException. But, so far, the introduction of Optional helped to accomplish it only to a degree and mostly in the area of streams and functional programming.
The vision that motivated the creation of the Optional class was to call the isPresent() method on an Optional object and then apply the get() method (to get the contained value) only when the isPresent() method returns true. Unfortunately, when one cannot guarantee that the reference to the Optional object itself is not null, one needs to check it in order to avoid NullPointerException. If so, then the value of using Optional
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access