Effective Java! Return Optionals Judiciously
Prior to Java 8 if a method didn’t want to return a value some of the time and return a value at other times there were a few options. The method could return a null
, the method could throw an exception, or you could come up with some state holding object that could be returned (although I've never seen this). The first two options aren't great. Returning nulls
is just asking for Null pointer exceptions…