Handling Null: Optional and Nullable Types

Java has long been infamous for its NullPointerException. The reason for the NPE is calling a method or accessing an attribute of an object that has not been initialized.

Java
 
var value = foo.getBar().getBaz().toLowerCase();

Running this snippet may result in something like the following:

Using Java Optional Vs. Vavr Option

When it comes to Java, there are so many Options...

Today, I would like to discuss an essential Java topic – the usage of Optional class — and compare it with an alternative from the Vavr library. Optional was initially introduced in Java 8 and defined as “a container object which may or may not contain a non-null value.”

You may also like: 26 Reasons Why Using Optional Correctly Is Not Optional

Developers utilize Optionals in order to avoid null checking in places when code execution leads to "not a result" but also to a null value, and it can, in this regard, result in a NullPointerException. In such cases, Optional offers us some fancy functionality, but not all of it was introduced in the 8th release, some features require Java 11. Another way to handle these issues is with Vavr’s Option class.