Java Optional and Either: Handling Null Values and Representing Two Possible Values

Samuel Catalano
The Fresh Writes
Published in
4 min readMar 10, 2023

Handling null values can be a challenge in Java, as null values can cause null pointer exceptions. However, Java provides two classes that can help with this problem: Optional and Either. In this post, we’ll explore the differences between these two classes, and when to use each one.

Java Optional: Handling Null Values

Java Optional is a container object that may or may not contain a non-null value. Introduced in Java 8, Optional provides a way to handle null values without throwing a NullPointerException.

The Optional class provides two methods: isPresent() and get(). The isPresent() method returns true if the Optional contains a non-null value, and false otherwise. The get() method returns the value if it is present, otherwise, it throws a NoSuchElementException.

Java Optional is a better alternative to returning null values, which can cause null pointer exceptions. Optional can be used in a variety of scenarios, such as returning a default value if a value is not present. For example:

Optional<String> optionalName = Optional.ofNullable(name);
String displayName = optionalName.orElse("Unknown");

In this example, optionalName is an Optional that contains a value if name is not null, otherwise it is empty. The orElse() method returns the value if it is present, otherwise it returns the default value “Unknown”.

Java Either: Representing Two Possible Values

Java Either is a class that provides a way to represent one of two possible values. While Optional handles null values, Either can handle two possible outcomes.

Java Either is not a part of the Java Standard Library, but it can be found in many Java libraries, including Vavr and Apache Commons Lang.

Java Either provides two methods: isLeft() and isRight(). The isLeft() method returns true if the value is the left value, and false otherwise. The isRight() method returns true if the value is the right value, and false otherwise.

Java Either can be used in a variety of scenarios, such as returning two possible values from a method. For example:

public Either<String, Integer> divide(int x, int y) {
if (y == 0) {
return Either.left("Cannot divide by zero");
} else {
return Either.right(x / y);
}
}

In this example, the divide() method returns an Either that contains either a String (the error message) or an Integer (the result of the division). If y is 0, the method returns a left value containing the error message. Otherwise, it returns a right value containing the result of the division.

When to Use Java Optional and Either

Java Optional and Either serve different purposes and should be used in different scenarios.

Use Java Optional when you need to handle null values. Optional is a good choice when you need to return a default value if a value is not present.

Use Java Either when you need to represent one of two possible values. Either is a good choice when you need to return two possible values from a method or handle two possible outcomes.

Conclusion

Java Optional and Either are two useful classes in Java that can be used to handle null values and represent one of two possible values. While Optional is used to handle null values, Either is used to represent two possible outcomes. By using these classes, you can avoid null pointer exceptions and handle different scenarios with ease.

Do support our publication by following it

Also refer to the following articles

--

--

Samuel Catalano
The Fresh Writes

Samuel is a Software Engineer from Brazil with main interests in Java, Spring Boot, Quarkus, Microservices, Docker, Databases, Kubernetes, and Clean Code