Optional used as method argument?

Remi Forax forax at univ-mlv.fr
Fri Oct 2 08:10:23 UTC 2015


Hi Max,
yes, It's usually not a good idea,
creating an Optional is like a boxing, you create an object (and prey that escape analysis will work),
usually null means there is no value so either you have two methods consume, something like consumeAValue(String) and consumeNoValue() or you have one method consumeAValue and you don't call it if there is no value.

regards,
Rémi

----- Mail original -----
> De: "Wang Weijun" <weijun.wang at oracle.com>
> À: core-libs-dev at openjdk.java.net
> Envoyé: Vendredi 2 Octobre 2015 03:25:43
> Objet: Optional used as method argument?
> 
> I hear people saying Optional is usually used as return values. Can I use it
> as an argument, like this?
> 
>   void consume(Optional<String> value)
> 
> This way, I don't need to add spec like "@param value can be null if there is
> no such value".
> 
> Thanks
> Max
> 
> 



More information about the core-libs-dev mailing list