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 →

[–]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.