< prev index next > src/java.base/share/classes/java/lang/Short.java
Print this page
/*
! * Copyright (c) 1996, 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
/*
! * Copyright (c) 1996, 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
*/
package java.lang;
import jdk.internal.misc.CDS;
+ import jdk.internal.misc.PreviewFeatures;
+ import jdk.internal.value.DeserializeConstructor;
+ import jdk.internal.value.ValueClass;
import jdk.internal.vm.annotation.AOTSafeClassInitializer;
import jdk.internal.vm.annotation.IntrinsicCandidate;
import jdk.internal.vm.annotation.Stable;
import java.lang.constant.Constable;
* a {@code short} to a {@code String} and a {@code String} to a
* {@code short}, as well as other constants and methods useful when
* dealing with a {@code short}.
*
* <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.
*
* @see java.lang.Number
* @since 1.1
*/
@jdk.internal.ValueBased
public final class Short extends Number implements Comparable<Short>, Constable {
/**
* A constant holding the minimum value a {@code short} can
* a {@code short} to a {@code String} and a {@code String} to a
* {@code short}, as well as other constants and methods useful when
* dealing with a {@code short}.
*
* <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, mutexes, or
! * with {@linkplain java.lang.ref.Reference object references}.
! *
+ * <div class="preview-block">
+ * <div class="preview-comment">
+ * When preview features are enabled, {@code Short} 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>
*
* @see java.lang.Number
* @since 1.1
*/
+ @jdk.internal.MigratedValueClass
@jdk.internal.ValueBased
public final class Short extends Number implements Comparable<Short>, Constable {
/**
* A constant holding the minimum value a {@code short} can
int size = -(-128) + 127 + 1;
// Load and use the archived cache if it exists
CDS.initializeFromArchive(ShortCache.class);
if (archivedCache == null) {
! Short[] c = new Short[size];
short value = -128;
for(int i = 0; i < size; i++) {
c[i] = new Short(value++);
}
archivedCache = c;
}
cache = archivedCache;
assert cache.length == size;
}
}
/**
* Returns a {@code Short} instance representing the specified
* {@code short} value.
! * If a new {@code Short} instance is not required, this method
! * should generally be used in preference to the constructor
! * {@link #Short(short)}, 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.
*
* @param s a short value.
* @return a {@code Short} instance representing {@code s}.
* @since 1.5
*/
int size = -(-128) + 127 + 1;
// Load and use the archived cache if it exists
CDS.initializeFromArchive(ShortCache.class);
if (archivedCache == null) {
! Short[] c = newCacheArray(size);
short value = -128;
for(int i = 0; i < size; i++) {
c[i] = new Short(value++);
}
archivedCache = c;
}
cache = archivedCache;
assert cache.length == size;
}
+
+ private static Short[] newCacheArray(int size) {
+ // ValueClass.newReferenceArray requires a value class component.
+ if (PreviewFeatures.isEnabled()) {
+ return (Short[]) ValueClass.newReferenceArray(Short.class, size);
+ }
+ return new Short[size];
+ }
}
/**
* Returns a {@code Short} instance representing the specified
* {@code short} value.
! * <div class="preview-block">
! * <div class="preview-comment">
! * <p>
! * - When preview features are NOT enabled, {@code Short} is an identity class.
! * If a new {@code Short} instance is not required, this method
! * should generally be used in preference to the constructor
! * {@link #Short(short)}, 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 Short} 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 s a short value.
* @return a {@code Short} instance representing {@code s}.
* @since 1.5
*/
* It is rarely appropriate to use this constructor. The static factory
* {@link #valueOf(short)} is generally a better choice, as it is
* likely to yield significantly better space and time performance.
*/
@Deprecated(since="9")
+ @DeserializeConstructor
public Short(short value) {
this.value = value;
}
/**
< prev index next >