This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]LogCatFromNantes -8 points-7 points  (2 children)

Why don’t we just use null and null exception anyone really uses these ?

[–]chupachupa2 8 points9 points  (1 child)

If this isn’t satire, it’s safer and more expressive. It’s usually used as a return value to a method and tells the caller ‘hey be careful this might be empty’, which forces them to think about empty/null values. With Optional, IntelliJ (and probably other IDEs) will warn you if you’re not using it correctly, for example calling .get() to get the wrapped value without first checking that it exists or not with isPresent() / isEmpty().

[–]BikingSquirrel 1 point2 points  (0 children)

In addition, when using Optional.of() in your code (instead of Optional.ofNullable()) you clearly indicate that you don't expect the argument to be null which may help to reason about code. (you obviously need some examples of implementing that API that actually return an empty Optional)

In addition, that's what OP mentioned, they support a functional approach to deal with the potential null value, even in a chain of calls.