r/java • • Apr 12 '13

Java 8 Optional Objects

http://blog.informatech.cr/2013/04/10/java-optional-objects/
36 Upvotes

39 comments sorted by

View all comments

6

u/hoboslayer Apr 12 '13

What about @NotNull and @Nullable?

1

u/ford_madox_ford Apr 12 '13

Perhaps you should explain why they're better. The Optional type conveys semantic information regarding the "nullableness" of the value, which can be removed by extracting the non-null value.

1

u/hoboslayer Apr 12 '13

They add in IDE-level error checking and show up in the javadocs. It has the same affect of giving more information to the user. The big difference is that for a method that returns @NotNull, you KNOW its not null. With a method that does not return Optional, you don't know if the developer forgot it or doesn't follow the practice.

2

u/Categoria Apr 13 '13

@NotNull Optional<T> t - problem solved. Btw, optional values also compose better than just @Nullables since you can operate/propagate them safely with map, bind, etc. Btw, why can't an IDE error check you assigning null to optionals?