< prev index next >

src/java.base/share/classes/java/lang/Integer.java

Print this page
@@ -24,12 +24,15 @@
   */
  
  package java.lang;
  
  import jdk.internal.misc.CDS;
+ import jdk.internal.misc.PreviewFeatures;
  import jdk.internal.misc.VM;
  import jdk.internal.util.DecimalDigits;
+ import jdk.internal.value.DeserializeConstructor;
+ import jdk.internal.value.ValueClass;
  import jdk.internal.vm.annotation.AOTRuntimeSetup;
  import jdk.internal.vm.annotation.AOTSafeClassInitializer;
  import jdk.internal.vm.annotation.ForceInline;
  import jdk.internal.vm.annotation.IntrinsicCandidate;
  import jdk.internal.vm.annotation.Stable;

@@ -54,24 +57,33 @@
   * an {@code int} to a {@code String} and a {@code String} to an
   * {@code int}, as well as other constants and methods useful when
   * dealing with an {@code int}.
   *
   * <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.
+  * class; programmers should treat instances that are {@linkplain #equals(Object) equal}
+  * as interchangeable and should not use instances for synchronization, mutexes, or
+  * with {@linkplain java.lang.ref.Reference object references}.
+  *
+  * <div class="preview-block">
+  *      <div class="preview-comment">
+  *          When preview features are enabled, {@code Integer} is a {@linkplain Class#isValue value class}.
+  *          Use of value class instances for synchronization, mutexes, or with
+  *          {@linkplain java.lang.ref.Reference object references} result in
+  *          {@link IdentityException}.
+  *      </div>
+  * </div>
   *
   * <p>Implementation note: The implementations of the "bit twiddling"
   * methods (such as {@link #highestOneBit(int) highestOneBit} and
   * {@link #numberOfTrailingZeros(int) numberOfTrailingZeros}) are
   * based on material from Henry S. Warren, Jr.'s <cite>Hacker's
   * Delight</cite>, (Addison Wesley, 2002) and <cite>Hacker's
   * Delight, Second Edition</cite>, (Pearson Education, 2013).
   *
   * @since 1.0
   */
+ @jdk.internal.MigratedValueClass
  @jdk.internal.ValueBased
  public final class Integer extends Number
          implements Comparable<Integer>, Constable, ConstantDesc {
      /**
       * A constant holding the minimum value an {@code int} can

@@ -920,11 +932,11 @@
                      // If the property cannot be parsed into an int, ignore it.
                  }
              }
              high = h;
  
-             Integer[] precomputed = null;
+             Integer[] precomputed;
              if (cache != null) {
                  // IntegerCache has been AOT-initialized.
                  precomputed = cache;
              } else {
                  // Legacy CDS archive support (to be deprecated):

@@ -945,11 +957,11 @@
              // Use the precomputed cache if it exists and is large enough
              if (precomputed != null && size <= precomputed.length) {
                  return precomputed;
              }
  
-             Integer[] c = new Integer[size];
+             Integer[] c = newCacheArray(size);
              int j = low;
              // If we loading a precomputed cache (from AOT cache or CDS archive),
              // we must use all instances from it.
              // Otherwise, the Integers from the AOT cache (or CDS archive) will not
              // have the same object identity as items in IntegerCache.cache[].

@@ -964,23 +976,43 @@
                  c[i] = new Integer(j++);
              }
              return c;
          }
  
+         private static Integer[] newCacheArray(int size) {
+             // ValueClass.newReferenceArray requires a value class component.
+             if (PreviewFeatures.isEnabled()) {
+                 return (Integer[]) ValueClass.newReferenceArray(Integer.class, size);
+             }
+             return new Integer[size];
+         }
+ 
          private IntegerCache() {}
      }
  
      /**
       * Returns an {@code Integer} instance representing the specified
-      * {@code int} value.  If a new {@code Integer} instance is not
-      * required, this method should generally be used in preference to
-      * the constructor {@link #Integer(int)}, as this method is likely
-      * to yield significantly better space and time performance by
-      * caching frequently requested values.
-      *
-      * This method will always cache values in the range -128 to 127,
-      * inclusive, and may cache other values outside of this range.
+      * {@code int} value.
+      * <div class="preview-block">
+      *      <div class="preview-comment">
+      *          <p>
+      *              - When preview features are NOT enabled, {@code Integer} is an identity class.
+      *              If a new {@code Integer} instance is not
+      *              required, this method should generally be used in preference to
+      *              the constructor {@link #Integer(int)}, as this method is likely
+      *              to yield significantly better space and time performance by
+      *              caching frequently requested values.
+      *              This method will always cache values in the range -128 to 127,
+      *              inclusive, and may cache other values outside of this range.
+      *          </p>
+      *          <p>
+      *              - When preview features are enabled, {@code Integer} is a {@linkplain Class#isValue value class}.
+      *              The {@code valueOf} behavior is the same as invoking the constructor,
+      *              whether cached or not.
+      *          </p>
+      *      </div>
+      * </div>
       *
       * @param  i an {@code int} value.
       * @return an {@code Integer} instance representing {@code i}.
       * @since  1.5
       */

@@ -1009,10 +1041,11 @@
       * It is rarely appropriate to use this constructor. The static factory
       * {@link #valueOf(int)} is generally a better choice, as it is
       * likely to yield significantly better space and time performance.
       */
      @Deprecated(since="9")
+     @DeserializeConstructor
      public Integer(int value) {
          this.value = value;
      }
  
      /**
< prev index next >