java.util.Optional fields

Peter Levart peter.levart at
Fri Sep 21 04:35:47 PDT 2012

Hi all,

I'm just thinking about the implementation of current java.util.Optional.

It has 2 fields: "T value" and "boolean present".

But since the invariant is:

     public Optional(T value) {
         this.value = Objects.requireNonNull(value);
         this.present = true;

     private Optional() {
         this.value = null;
         this.present = false;

// value != null && present || value == null && !present.

Isn't the "present" flag superfluous? Or is this intentional? Is the 
test for boolean faster that the check for reference equality with null? 
Does additional boolean flag, considering alignment constraints, produce 
a larger object or does it not?

Regards, Peter

More information about the lambda-dev mailing list