< prev index next >

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

Print this page
*** 1,7 ***
  /*
!  * Copyright (c) 2012, 2025, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Oracle designates this
--- 1,7 ---
  /*
!  * Copyright (c) 2012, 2026, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Oracle designates this

*** 44,14 ***
   * (returns a default value if no value is present) and
   * {@link #ifPresent(Consumer) ifPresent()} (performs an
   * action if a value is present).
   *
   * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
!  * class; programmers should treat instances that are
!  * {@linkplain #equals(Object) equal} as interchangeable and should not
!  * use instances for synchronization, or unpredictable behavior may
!  * occur. For example, in a future release, synchronization may fail.
   *
   * @apiNote
   * {@code Optional} is primarily intended for use as a method return type where
   * there is a clear need to represent "no result," and where using {@code null}
   * is likely to cause errors. A variable whose type is {@code Optional} should
--- 44,22 ---
   * (returns a default value if no value is present) and
   * {@link #ifPresent(Consumer) ifPresent()} (performs an
   * action if a value is present).
   *
   * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
!  * class; programmers should treat instances that are {@linkplain #equals(Object) equal}
!  * as interchangeable and should not use instances for synchronization or
!  * with {@linkplain java.lang.ref.Reference object references}.
!  *
+  * <div class="preview-block">
+  *      <div class="preview-comment">
+  *          When preview features are enabled, {@code Optional} is a {@linkplain Class#isValue value class}.
+  *          Use of value class instances for synchronization or with
+  *          {@linkplain java.lang.ref.Reference object references} result in
+  *          {@link IdentityException}.
+  *      </div>
+  * </div>
   *
   * @apiNote
   * {@code Optional} is primarily intended for use as a method return type where
   * there is a clear need to represent "no result," and where using {@code null}
   * is likely to cause errors. A variable whose type is {@code Optional} should

*** 61,11 ***
   * @param <T> the type of value
   * @since 1.8
   */
  @jdk.internal.ValueBased
  @TrustFinalFields
! public final class Optional<T> {
      /**
       * Common instance for {@code empty()}.
       */
      private static final Optional<?> EMPTY = new Optional<>(null);
  
--- 69,11 ---
   * @param <T> the type of value
   * @since 1.8
   */
  @jdk.internal.ValueBased
  @TrustFinalFields
! public final /*value*/ class Optional<T> {
      /**
       * Common instance for {@code empty()}.
       */
      private static final Optional<?> EMPTY = new Optional<>(null);
  
< prev index next >