I\'ve read here why Optional.of()
should be used over Optional.ofNullable()
, but the answer didn\'t satisfy me at all, so I ask slightly different:
Angelika Langer says that Optional.ofNullable
is only a convenience-method, calling the other both static methods from Optional
. It is implemented as:
return value == null ? empty() : of(value) ;
Also she says that Optional.ofNullable
was added lately to the API.
Here is her text in german language: http://www.angelikalanger.com/Articles/EffectiveJava/80.Java8.Optional-Result/80.Java8.Optional-Result.html
So I would use Optional.of
only when null is an error, which should be found early. This is what Tagir Valeev said in:
Why use Optional.of over Optional.ofNullable?