Earlier quoted context omitted.
The problem is that you don't get guarantees that something is not null; you can still do Optional myOptional = null. Second, you're not required to handle the not-present case. Third, if you attempt to get the value of an optional (using .get()) when it is not present, you get an (unchecked, so no requirement to catch) exception - basically replacing NullPointerException with NoSuchElementException. The only value O…
At least one of the values of Optional is to communicate in the API that a value can be missing. The article mentions that: A programmer looking at the code for the first time will know, just by looking at the return type, "Hey, this method may not return a record! I'll have to handle that scenario." Making this clear to users of an API is something that nullable properties can not do. The user has to deliberately ig…
Well, that's basically the same thing with any nullable return then. Wether I return T or Optional, there might be no record.
Even worse, when I return T, you only have 2 cases : I return something, or I return null.
With optional, you have 3 cases to check : I returned Something, I returned Optional.of(null), or I returned null.