1 /*
2 * Copyright (c) 1996, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
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_short;
39 import static java.lang.constant.ConstantDescs.DEFAULT_NAME;
40
41 /**
42 * The {@code Short} class is the {@linkplain
43 * java.lang##wrapperClass wrapper class} for values of the primitive
44 * type {@code short}. An object of type {@code Short} contains a
45 * single field whose type is {@code short}.
46 *
47 * <p>In addition, this class provides several methods for converting
48 * a {@code short} to a {@code String} and a {@code String} to a
49 * {@code short}, as well as other constants and methods useful when
50 * dealing with a {@code short}.
51 *
52 * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
53 * class; programmers should treat instances that are
54 * {@linkplain #equals(Object) equal} as interchangeable and should not
55 * use instances for synchronization, or unpredictable behavior may
56 * occur. For example, in a future release, synchronization may fail.
57 *
58 * @see java.lang.Number
59 * @since 1.1
60 */
61 @jdk.internal.ValueBased
62 public final class Short extends Number implements Comparable<Short>, Constable {
63
64 /**
65 * A constant holding the minimum value a {@code short} can
66 * have, -2<sup>15</sup>.
67 */
68 public static final short MIN_VALUE = -32768;
69
70 /**
71 * A constant holding the maximum value a {@code short} can
72 * have, 2<sup>15</sup>-1.
73 */
74 public static final short MAX_VALUE = 32767;
75
76 /**
77 * The {@code Class} instance representing the primitive type
78 * {@code short}.
79 */
80 public static final Class<Short> TYPE = Class.getPrimitiveClass("short");
81
82 /**
227 */
228 @Override
229 public Optional<DynamicConstantDesc<Short>> describeConstable() {
230 return Optional.of(DynamicConstantDesc.ofNamed(BSM_EXPLICIT_CAST, DEFAULT_NAME, CD_short, intValue()));
231 }
232
233 @AOTSafeClassInitializer
234 private static final class ShortCache {
235 private ShortCache() {}
236
237 @Stable
238 static final Short[] cache;
239 static Short[] archivedCache;
240
241 static {
242 int size = -(-128) + 127 + 1;
243
244 // Load and use the archived cache if it exists
245 CDS.initializeFromArchive(ShortCache.class);
246 if (archivedCache == null) {
247 Short[] c = new Short[size];
248 short value = -128;
249 for(int i = 0; i < size; i++) {
250 c[i] = new Short(value++);
251 }
252 archivedCache = c;
253 }
254 cache = archivedCache;
255 assert cache.length == size;
256 }
257 }
258
259 /**
260 * Returns a {@code Short} instance representing the specified
261 * {@code short} value.
262 * If a new {@code Short} instance is not required, this method
263 * should generally be used in preference to the constructor
264 * {@link #Short(short)}, as this method is likely to yield
265 * significantly better space and time performance by caching
266 * frequently requested values.
267 *
268 * This method will always cache values in the range -128 to 127,
269 * inclusive, and may cache other values outside of this range.
270 *
271 * @param s a short value.
272 * @return a {@code Short} instance representing {@code s}.
273 * @since 1.5
274 */
275 @IntrinsicCandidate
276 public static Short valueOf(short s) {
277 final int offset = 128;
278 int sAsInt = s;
279 if (sAsInt >= -128 && sAsInt <= 127) { // must cache
280 return ShortCache.cache[sAsInt + offset];
281 }
282 return new Short(s);
283 }
284
285 /**
286 * Decodes a {@code String} into a {@code Short}.
287 * Accepts decimal, hexadecimal, and octal numbers given by
288 * the following grammar:
289 *
335 /**
336 * The value of the {@code Short}.
337 *
338 * @serial
339 */
340 private final short value;
341
342 /**
343 * Constructs a newly allocated {@code Short} object that
344 * represents the specified {@code short} value.
345 *
346 * @param value the value to be represented by the
347 * {@code Short}.
348 *
349 * @deprecated
350 * It is rarely appropriate to use this constructor. The static factory
351 * {@link #valueOf(short)} is generally a better choice, as it is
352 * likely to yield significantly better space and time performance.
353 */
354 @Deprecated(since="9")
355 public Short(short value) {
356 this.value = value;
357 }
358
359 /**
360 * Constructs a newly allocated {@code Short} object that
361 * represents the {@code short} value indicated by the
362 * {@code String} parameter. The string is converted to a
363 * {@code short} value in exactly the manner used by the
364 * {@code parseShort} method for radix 10.
365 *
366 * @param s the {@code String} to be converted to a
367 * {@code Short}
368 * @throws NumberFormatException If the {@code String}
369 * does not contain a parsable {@code short}.
370 *
371 * @deprecated
372 * It is rarely appropriate to use this constructor.
373 * Use {@link #parseShort(String)} to convert a string to a
374 * {@code short} primitive, or use {@link #valueOf(String)}
|
1 /*
2 * Copyright (c) 1996, 2026, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
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.Deserializer;
31 import jdk.internal.value.ValueClass;
32 import jdk.internal.vm.annotation.AOTSafeClassInitializer;
33 import jdk.internal.vm.annotation.IntrinsicCandidate;
34 import jdk.internal.vm.annotation.Stable;
35
36 import java.lang.constant.Constable;
37 import java.lang.constant.DynamicConstantDesc;
38 import java.util.Optional;
39
40 import static java.lang.constant.ConstantDescs.BSM_EXPLICIT_CAST;
41 import static java.lang.constant.ConstantDescs.CD_short;
42 import static java.lang.constant.ConstantDescs.DEFAULT_NAME;
43
44 /**
45 * The {@code Short} class is the {@linkplain
46 * java.lang##wrapperClass wrapper class} for values of the primitive
47 * type {@code short}. An object of type {@code Short} contains a
48 * single field whose type is {@code short}.
49 *
50 * <p>In addition, this class provides several methods for converting
51 * a {@code short} to a {@code String} and a {@code String} to a
52 * {@code short}, as well as other constants and methods useful when
53 * dealing with a {@code short}.
54 *
55 * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
56 * class; programmers should treat instances that are {@linkplain #equals(Object) equal}
57 * as interchangeable and should not use instances for synchronization or
58 * with {@linkplain java.lang.ref.Reference object references}.
59 *
60 * <div class="preview-block">
61 * <div class="preview-comment">
62 * When preview features are enabled, {@code Short} is a {@linkplain Class#isValue value class}.
63 * Use of value class instances for synchronization or with
64 * {@linkplain java.lang.ref.Reference object references} result in
65 * {@link IdentityException}.
66 * </div>
67 * </div>
68 *
69 * @see java.lang.Number
70 * @since 1.1
71 */
72 @jdk.internal.ValueBased
73 // See doc/value-class-preview.md for an overview of value class generation
74 public final /*value*/ class Short extends Number
75 implements Comparable<Short>, Constable {
76
77 /**
78 * A constant holding the minimum value a {@code short} can
79 * have, -2<sup>15</sup>.
80 */
81 public static final short MIN_VALUE = -32768;
82
83 /**
84 * A constant holding the maximum value a {@code short} can
85 * have, 2<sup>15</sup>-1.
86 */
87 public static final short MAX_VALUE = 32767;
88
89 /**
90 * The {@code Class} instance representing the primitive type
91 * {@code short}.
92 */
93 public static final Class<Short> TYPE = Class.getPrimitiveClass("short");
94
95 /**
240 */
241 @Override
242 public Optional<DynamicConstantDesc<Short>> describeConstable() {
243 return Optional.of(DynamicConstantDesc.ofNamed(BSM_EXPLICIT_CAST, DEFAULT_NAME, CD_short, intValue()));
244 }
245
246 @AOTSafeClassInitializer
247 private static final class ShortCache {
248 private ShortCache() {}
249
250 @Stable
251 static final Short[] cache;
252 static Short[] archivedCache;
253
254 static {
255 int size = -(-128) + 127 + 1;
256
257 // Load and use the archived cache if it exists
258 CDS.initializeFromArchive(ShortCache.class);
259 if (archivedCache == null) {
260 Short[] c = newCacheArray(size);
261 short value = -128;
262 for(int i = 0; i < size; i++) {
263 c[i] = new Short(value++);
264 }
265 archivedCache = c;
266 }
267 cache = archivedCache;
268 assert cache.length == size;
269 }
270
271 private static Short[] newCacheArray(int size) {
272 // ValueClass.newReferenceArray requires a value class component.
273 if (PreviewFeatures.isEnabled()) {
274 return (Short[]) ValueClass.newReferenceArray(Short.class, size);
275 }
276 return new Short[size];
277 }
278 }
279
280 /**
281 * Returns a {@code Short} instance representing the specified
282 * {@code short} value.
283 * <div class="preview-block">
284 * <div class="preview-comment">
285 * <p>
286 * - When preview features are NOT enabled, {@code Short} is an identity class.
287 * If a new {@code Short} instance is not required, this method
288 * should generally be used in preference to the constructor
289 * {@link #Short(short)}, as this method is likely to yield
290 * significantly better space and time performance by caching
291 * frequently requested values.
292 * This method will always cache values in the range -128 to 127,
293 * inclusive, and may cache other values outside of this range.
294 * </p>
295 * <p>
296 * - When preview features are enabled, {@code Short} is a {@linkplain Class#isValue value class}.
297 * The {@code valueOf} behavior is the same as invoking the constructor,
298 * whether cached or not.
299 * </p>
300 * </div>
301 * </div>
302 *
303 * @param s a short value.
304 * @return a {@code Short} instance representing {@code s}.
305 * @since 1.5
306 */
307 @IntrinsicCandidate
308 public static Short valueOf(short s) {
309 final int offset = 128;
310 int sAsInt = s;
311 if (sAsInt >= -128 && sAsInt <= 127) { // must cache
312 return ShortCache.cache[sAsInt + offset];
313 }
314 return new Short(s);
315 }
316
317 /**
318 * Decodes a {@code String} into a {@code Short}.
319 * Accepts decimal, hexadecimal, and octal numbers given by
320 * the following grammar:
321 *
367 /**
368 * The value of the {@code Short}.
369 *
370 * @serial
371 */
372 private final short value;
373
374 /**
375 * Constructs a newly allocated {@code Short} object that
376 * represents the specified {@code short} value.
377 *
378 * @param value the value to be represented by the
379 * {@code Short}.
380 *
381 * @deprecated
382 * It is rarely appropriate to use this constructor. The static factory
383 * {@link #valueOf(short)} is generally a better choice, as it is
384 * likely to yield significantly better space and time performance.
385 */
386 @Deprecated(since="9")
387 @Deserializer("value")
388 public Short(short value) {
389 this.value = value;
390 }
391
392 /**
393 * Constructs a newly allocated {@code Short} object that
394 * represents the {@code short} value indicated by the
395 * {@code String} parameter. The string is converted to a
396 * {@code short} value in exactly the manner used by the
397 * {@code parseShort} method for radix 10.
398 *
399 * @param s the {@code String} to be converted to a
400 * {@code Short}
401 * @throws NumberFormatException If the {@code String}
402 * does not contain a parsable {@code short}.
403 *
404 * @deprecated
405 * It is rarely appropriate to use this constructor.
406 * Use {@link #parseShort(String)} to convert a string to a
407 * {@code short} primitive, or use {@link #valueOf(String)}
|