< prev index next >

src/java.base/share/classes/java/util/Optional.java

Print this page

 41  * (returns a default value if no value is present) and
 42  * {@link #ifPresent(Consumer) ifPresent()} (performs an
 43  * action if a value is present).
 44  *
 45  * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
 46  * class; programmers should treat instances that are
 47  * {@linkplain #equals(Object) equal} as interchangeable and should not
 48  * use instances for synchronization, or unpredictable behavior may
 49  * occur. For example, in a future release, synchronization may fail.
 50  *
 51  * @apiNote
 52  * {@code Optional} is primarily intended for use as a method return type where
 53  * there is a clear need to represent "no result," and where using {@code null}
 54  * is likely to cause errors. A variable whose type is {@code Optional} should
 55  * never itself be {@code null}; it should always point to an {@code Optional}
 56  * instance.
 57  *
 58  * @param <T> the type of value
 59  * @since 1.8
 60  */

 61 @jdk.internal.ValueBased
 62 public final class Optional<T> {
 63     /**
 64      * Common instance for {@code empty()}.
 65      */
 66     private static final Optional<?> EMPTY = new Optional<>(null);
 67 
 68     /**
 69      * If non-null, the value; if null, indicates no value is present
 70      */
 71     private final T value;
 72 
 73     /**
 74      * Returns an empty {@code Optional} instance.  No value is present for this
 75      * {@code Optional}.
 76      *
 77      * @apiNote
 78      * Though it may be tempting to do so, avoid testing if an object is empty
 79      * by comparing with {@code ==} or {@code !=} against instances returned by
 80      * {@code Optional.empty()}.  There is no guarantee that it is a singleton.

 41  * (returns a default value if no value is present) and
 42  * {@link #ifPresent(Consumer) ifPresent()} (performs an
 43  * action if a value is present).
 44  *
 45  * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
 46  * class; programmers should treat instances that are
 47  * {@linkplain #equals(Object) equal} as interchangeable and should not
 48  * use instances for synchronization, or unpredictable behavior may
 49  * occur. For example, in a future release, synchronization may fail.
 50  *
 51  * @apiNote
 52  * {@code Optional} is primarily intended for use as a method return type where
 53  * there is a clear need to represent "no result," and where using {@code null}
 54  * is likely to cause errors. A variable whose type is {@code Optional} should
 55  * never itself be {@code null}; it should always point to an {@code Optional}
 56  * instance.
 57  *
 58  * @param <T> the type of value
 59  * @since 1.8
 60  */
 61 @jdk.internal.MigratedValueClass
 62 @jdk.internal.ValueBased
 63 public final class Optional<T> {
 64     /**
 65      * Common instance for {@code empty()}.
 66      */
 67     private static final Optional<?> EMPTY = new Optional<>(null);
 68 
 69     /**
 70      * If non-null, the value; if null, indicates no value is present
 71      */
 72     private final T value;
 73 
 74     /**
 75      * Returns an empty {@code Optional} instance.  No value is present for this
 76      * {@code Optional}.
 77      *
 78      * @apiNote
 79      * Though it may be tempting to do so, avoid testing if an object is empty
 80      * by comparing with {@code ==} or {@code !=} against instances returned by
 81      * {@code Optional.empty()}.  There is no guarantee that it is a singleton.
< prev index next >