14
What are some things in the class library you wish more people knew about or used?
(docs.oracle.com)
For discussing Java, the JVM, languages that run on the JVM, and other related technologies.
IMHO it should be used for parameters, as long as there is more than one optional parameter. If rather all non nullable parameters though, which is usually doable.
I've waffled on it. Currently my opinion is to use whatever nullable annotation the project uses (if any, otherwise JetBrains's). Essentially, I'm not sure if the official recommendation to avoid
Optional
for uses other than return types has reasoning I'm missing.I do use it like this though and lament that we don't have an Elvis operator (
?:
)Instead of wrapping it in an optional you can do
Objects.requireNonNullElse(value, defaultValue)
Optional has more syntactic sugar for more complex scenarios / functional call chaining that prevents repetitive
if
checksThis is completely null safe, the function calls are only made if the object is not null