DEV Community

Discussion on: Be careful Of This Java Optional Method

Collapse
 
scottshipp profile image
scottshipp

Another one to watch for: Optional.of. It's easy to assume that the behavior of this method when passed null will be to return an empty Optional. Instead, it throws a NullPointerException.

The more verbose Optional.ofNullable is the one that returns an empty Optional when passed null.