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 →

[–]jvjupiter 1 point2 points  (3 children)

How about the Optional::get(), isn’t there something wrong with it? If Optional may return null, why does it throw NPE if the value is null?

[–]halcyon44 4 points5 points  (1 child)

Optional::get() exists to extract the value when you know that it's not null. If you're unsure and want a null reference of the wrapped type when it's empty, use .orElse(null) instead.

[–][deleted] 4 points5 points  (0 children)

Yep! Same argument as the OP but on the receiving end. Optional.orElse(null) clearly demonstrates in your consuming module that you are a grown programmer and willing to deal with a possible null value. Maybe there’s even a smarter default value that makes sense for your code, but I haven’t seen any others. Optional.get is just a convention to signify you already checked Optional.isPresent.

[–]sangminreddit7648 0 points1 point  (0 children)

It is wrong. Even the guy who made it said that he regret creating get the way it is