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

Optional.empty doc should suggest using isEmpty

    XMLWordPrintable

Details

    • b28

    Description

      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.


      Attachments

        Issue Links

          Activity

            People

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

              Dates

                Created:
                Updated:
                Resolved: