9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.lang;
27
28 import jdk.internal.misc.CDS;
29 import jdk.internal.vm.annotation.AOTSafeClassInitializer;
30 import jdk.internal.vm.annotation.IntrinsicCandidate;
31 import jdk.internal.vm.annotation.Stable;
32
33 import java.lang.constant.Constable;
34 import java.lang.constant.DynamicConstantDesc;
35 import java.util.Optional;
36
37 import static java.lang.constant.ConstantDescs.BSM_EXPLICIT_CAST;
38 import static java.lang.constant.ConstantDescs.CD_int;
39 import static java.lang.constant.ConstantDescs.CD_short;
40 import static java.lang.constant.ConstantDescs.DEFAULT_NAME;
41
42 /**
43 * The {@code Short} class is the {@linkplain
44 * java.lang##wrapperClass wrapper class} for values of the primitive
45 * type {@code short}. An object of type {@code Short} contains a
46 * single field whose type is {@code short}.
47 *
48 * <p>In addition, this class provides several methods for converting
49 * a {@code short} to a {@code String} and a {@code String} to a
50 * {@code short}, as well as other constants and methods useful when
51 * dealing with a {@code short}.
52 *
53 * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
54 * class; programmers should treat instances that are
55 * {@linkplain #equals(Object) equal} as interchangeable and should not
56 * use instances for synchronization, or unpredictable behavior may
57 * occur. For example, in a future release, synchronization may fail.
58 *
59 * @see java.lang.Number
60 * @since 1.1
61 */
62 @jdk.internal.ValueBased
63 public final class Short extends Number implements Comparable<Short>, Constable {
64
65 /**
66 * A constant holding the minimum value a {@code short} can
67 * have, -2<sup>15</sup>.
68 */
69 public static final short MIN_VALUE = -32768;
70
71 /**
72 * A constant holding the maximum value a {@code short} can
73 * have, 2<sup>15</sup>-1.
74 */
75 public static final short MAX_VALUE = 32767;
76
77 /**
78 * The {@code Class} instance representing the primitive type
79 * {@code short}.
80 */
81 public static final Class<Short> TYPE = Class.getPrimitiveClass("short");
243 int size = -(-128) + 127 + 1;
244
245 // Load and use the archived cache if it exists
246 CDS.initializeFromArchive(ShortCache.class);
247 if (archivedCache == null) {
248 Short[] c = new Short[size];
249 short value = -128;
250 for(int i = 0; i < size; i++) {
251 c[i] = new Short(value++);
252 }
253 archivedCache = c;
254 }
255 cache = archivedCache;
256 assert cache.length == size;
257 }
258 }
259
260 /**
261 * Returns a {@code Short} instance representing the specified
262 * {@code short} value.
263 * If a new {@code Short} instance is not required, this method
264 * should generally be used in preference to the constructor
265 * {@link #Short(short)}, as this method is likely to yield
266 * significantly better space and time performance by caching
267 * frequently requested values.
268 *
269 * This method will always cache values in the range -128 to 127,
270 * inclusive, and may cache other values outside of this range.
271 *
272 * @param s a short value.
273 * @return a {@code Short} instance representing {@code s}.
274 * @since 1.5
275 */
276 @IntrinsicCandidate
277 public static Short valueOf(short s) {
278 final int offset = 128;
279 int sAsInt = s;
280 if (sAsInt >= -128 && sAsInt <= 127) { // must cache
281 return ShortCache.cache[sAsInt + offset];
282 }
283 return new Short(s);
284 }
285
286 /**
287 * Decodes a {@code String} into a {@code Short}.
288 * Accepts decimal, hexadecimal, and octal numbers given by
289 * the following grammar:
290 *
291 * <blockquote>
292 * <dl>
293 * <dt><i>DecodableString:</i>
294 * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
295 * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
296 * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
297 * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
298 * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
299 *
300 * <dt><i>Sign:</i>
301 * <dd>{@code -}
|
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.lang;
27
28 import jdk.internal.misc.CDS;
29 import jdk.internal.misc.PreviewFeatures;
30 import jdk.internal.value.DeserializeConstructor;
31 import jdk.internal.vm.annotation.AOTSafeClassInitializer;
32 import jdk.internal.vm.annotation.IntrinsicCandidate;
33 import jdk.internal.vm.annotation.Stable;
34
35 import java.lang.constant.Constable;
36 import java.lang.constant.DynamicConstantDesc;
37 import java.util.Optional;
38
39 import static java.lang.constant.ConstantDescs.BSM_EXPLICIT_CAST;
40 import static java.lang.constant.ConstantDescs.CD_short;
41 import static java.lang.constant.ConstantDescs.DEFAULT_NAME;
42
43 /**
44 * The {@code Short} class is the {@linkplain
45 * java.lang##wrapperClass wrapper class} for values of the primitive
46 * type {@code short}. An object of type {@code Short} contains a
47 * single field whose type is {@code short}.
48 *
49 * <p>In addition, this class provides several methods for converting
50 * a {@code short} to a {@code String} and a {@code String} to a
51 * {@code short}, as well as other constants and methods useful when
52 * dealing with a {@code short}.
53 *
54 * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
55 * class; programmers should treat instances that are {@linkplain #equals(Object) equal}
56 * as interchangeable and should not use instances for synchronization, mutexes, or
57 * with {@linkplain java.lang.ref.Reference object references}.
58 *
59 * <div class="preview-block">
60 * <div class="preview-comment">
61 * When preview features are enabled, {@code Short} is a {@linkplain Class#isValue value class}.
62 * Use of value class instances for synchronization, mutexes, or with
63 * {@linkplain java.lang.ref.Reference object references} result in
64 * {@link IdentityException}.
65 * </div>
66 * </div>
67 *
68 * @see java.lang.Number
69 * @since 1.1
70 */
71 @jdk.internal.MigratedValueClass
72 @jdk.internal.ValueBased
73 public final class Short extends Number implements Comparable<Short>, Constable {
74
75 /**
76 * A constant holding the minimum value a {@code short} can
77 * have, -2<sup>15</sup>.
78 */
79 public static final short MIN_VALUE = -32768;
80
81 /**
82 * A constant holding the maximum value a {@code short} can
83 * have, 2<sup>15</sup>-1.
84 */
85 public static final short MAX_VALUE = 32767;
86
87 /**
88 * The {@code Class} instance representing the primitive type
89 * {@code short}.
90 */
91 public static final Class<Short> TYPE = Class.getPrimitiveClass("short");
253 int size = -(-128) + 127 + 1;
254
255 // Load and use the archived cache if it exists
256 CDS.initializeFromArchive(ShortCache.class);
257 if (archivedCache == null) {
258 Short[] c = new Short[size];
259 short value = -128;
260 for(int i = 0; i < size; i++) {
261 c[i] = new Short(value++);
262 }
263 archivedCache = c;
264 }
265 cache = archivedCache;
266 assert cache.length == size;
267 }
268 }
269
270 /**
271 * Returns a {@code Short} instance representing the specified
272 * {@code short} value.
273 * <div class="preview-block">
274 * <div class="preview-comment">
275 * <p>
276 * - When preview features are NOT enabled, {@code Short} is an identity class.
277 * If a new {@code Short} instance is not required, this method
278 * should generally be used in preference to the constructor
279 * {@link #Short(short)}, as this method is likely to yield
280 * significantly better space and time performance by caching
281 * frequently requested values.
282 * This method will always cache values in the range -128 to 127,
283 * inclusive, and may cache other values outside of this range.
284 * </p>
285 * <p>
286 * - When preview features are enabled, {@code Short} is a {@linkplain Class#isValue value class}.
287 * The {@code valueOf} behavior is the same as invoking the constructor,
288 * whether cached or not.
289 * </p>
290 * </div>
291 * </div>
292 *
293 * @param s a short value.
294 * @return a {@code Short} instance representing {@code s}.
295 * @since 1.5
296 */
297 @IntrinsicCandidate
298 @DeserializeConstructor
299 public static Short valueOf(short s) {
300 if (!PreviewFeatures.isEnabled()) {
301 final int offset = 128;
302 int sAsInt = s;
303 if (sAsInt >= -128 && sAsInt <= 127) { // must cache
304 return ShortCache.cache[sAsInt + offset];
305 }
306 }
307 return new Short(s);
308 }
309
310 /**
311 * Decodes a {@code String} into a {@code Short}.
312 * Accepts decimal, hexadecimal, and octal numbers given by
313 * the following grammar:
314 *
315 * <blockquote>
316 * <dl>
317 * <dt><i>DecodableString:</i>
318 * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
319 * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
320 * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
321 * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
322 * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
323 *
324 * <dt><i>Sign:</i>
325 * <dd>{@code -}
|