Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8228615

Optional.empty doc should suggest using isEmpty

XMLWordPrintable

    • b28

      A DESCRIPTION OF THE PROBLEM :
      The documentation of java.util.Optional.empty() says:
      >Though it may be tempting to do so, avoid testing if an object is empty by comparing with == against instances returned by Optional.empty(). There is no guarantee that it is a singleton. Instead, use isPresent().

      This dates back to when there was no `isEmpty()`, however in Java 11 this method was added (JDK-8184693). Therefore the documentation for `empty()` should refer only, or additionally, to `isEmpty()`:
      >Though it may be tempting to do so, avoid testing if an object is empty by comparing with == against instances returned by Optional.empty(). There is no guarantee that it is a singleton. Instead, use isEmpty().

      Same for OptionalInt, OptionalDouble and OptionalLong.


            smarks Stuart Marks
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: