Optional require(s) NonNull

Brian Goetz brian.goetz at oracle.com
Mon Oct 29 08:15:27 PDT 2012


This constructor is for creating a *present* Optional.  You use Optional.empty() to create an *empty* optional.  

On Oct 29, 2012, at 11:02 AM, Boaz Nahum wrote:

> I thought I understand Optional till I read the implementation:
> 
> public Optional(T value) {
>        this.value = Objects.requireNonNull(value);
>        this.present = true;
> }
> 
> Why 'requireNonNull' non null ? If null is singular value why we need this
> class ? just for 'orElse..' methods ?
> 
> If if value can't be null why we need the 'present' field ?
> 
> My hopes where that I can use Optional to return a null as a legal value.
> 
> Thanks
> Boaz
> 



More information about the lambda-dev mailing list