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 /**
213 * represented by the string argument
214 * @throws NumberFormatException If the {@code String} does
215 * not contain a parsable {@code short}.
216 */
217 public static Short valueOf(String s) throws NumberFormatException {
218 return valueOf(s, 10);
219 }
220
221 /**
222 * Returns an {@link Optional} containing the nominal descriptor for this
223 * instance.
224 *
225 * @return an {@link Optional} describing the {@linkplain Short} instance
226 * @since 15
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 /**
226 * represented by the string argument
227 * @throws NumberFormatException If the {@code String} does
228 * not contain a parsable {@code short}.
229 */
230 public static Short valueOf(String s) throws NumberFormatException {
231 return valueOf(s, 10);
232 }
233
234 /**
235 * Returns an {@link Optional} containing the nominal descriptor for this
236 * instance.
237 *
238 * @return an {@link Optional} describing the {@linkplain Short} instance
239 * @since 15
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 // When preview features are enabled, the cache does not affect object
247 // equality == semantics, but exists for performance.
248 // See doc/value-class-preview.md "Wrapper Class Caches" section.
249 @AOTSafeClassInitializer
250 private static final class ShortCache {
251 private ShortCache() {}
252
253 @Stable
254 static final Short[] cache;
255 static Short[] archivedCache;
256
257 static {
258 int size = -(-128) + 127 + 1;
259
260 // Load and use the archived cache if it exists
261 CDS.initializeFromArchive(ShortCache.class);
262 if (archivedCache == null) {
263 Short[] c = newCacheArray(size);
264 short value = -128;
265 for(int i = 0; i < size; i++) {
266 c[i] = new Short(value++);
267 }
268 archivedCache = c;
269 }
270 cache = archivedCache;
271 assert cache.length == size;
272 }
273
274 private static Short[] newCacheArray(int size) {
275 // ValueClass.newReferenceArray requires a value class component.
276 if (PreviewFeatures.isEnabled()) {
277 return (Short[]) ValueClass.newReferenceArray(Short.class, size);
278 }
279 return new Short[size];
280 }
281 }
282
283 /**
284 * Returns a {@code Short} instance representing the specified
285 * {@code short} value.
286 * <div class="preview-block">
287 * <div class="preview-comment">
288 * <p>
289 * - When preview features are NOT enabled, {@code Short} is an identity class.
290 * If a new {@code Short} instance is not required, this method
291 * should generally be used in preference to the constructor
292 * {@link #Short(short)}, as this method is likely to yield
293 * significantly better space and time performance by caching
294 * frequently requested values.
295 * This method will always cache values in the range -128 to 127,
296 * inclusive, and may cache other values outside of this range.
297 * </p>
298 * <p>
299 * - When preview features are enabled, {@code Short} is a {@linkplain Class#isValue value class}.
300 * The {@code valueOf} behavior is the same as invoking the constructor,
301 * whether cached or not.
302 * </p>
303 * </div>
304 * </div>
305 *
306 * @param s a short value.
307 * @return a {@code Short} instance representing {@code s}.
308 * @since 1.5
309 */
310 @IntrinsicCandidate
311 public static Short valueOf(short s) {
312 final int offset = 128;
313 int sAsInt = s;
314 if (sAsInt >= -128 && sAsInt <= 127) { // must cache
315 return ShortCache.cache[sAsInt + offset];
316 }
317 return new Short(s);
318 }
319
320 /**
321 * Decodes a {@code String} into a {@code Short}.
322 * Accepts decimal, hexadecimal, and octal numbers given by
323 * the following grammar:
324 *
370 /**
371 * The value of the {@code Short}.
372 *
373 * @serial
374 */
375 private final short value;
376
377 /**
378 * Constructs a newly allocated {@code Short} object that
379 * represents the specified {@code short} value.
380 *
381 * @param value the value to be represented by the
382 * {@code Short}.
383 *
384 * @deprecated
385 * It is rarely appropriate to use this constructor. The static factory
386 * {@link #valueOf(short)} is generally a better choice, as it is
387 * likely to yield significantly better space and time performance.
388 */
389 @Deprecated(since="9")
390 @Deserializer("value")
391 public Short(short value) {
392 this.value = value;
393 }
394
395 /**
396 * Constructs a newly allocated {@code Short} object that
397 * represents the {@code short} value indicated by the
398 * {@code String} parameter. The string is converted to a
399 * {@code short} value in exactly the manner used by the
400 * {@code parseShort} method for radix 10.
401 *
402 * @param s the {@code String} to be converted to a
403 * {@code Short}
404 * @throws NumberFormatException If the {@code String}
405 * does not contain a parsable {@code short}.
406 *
407 * @deprecated
408 * It is rarely appropriate to use this constructor.
409 * Use {@link #parseShort(String)} to convert a string to a
410 * {@code short} primitive, or use {@link #valueOf(String)}
|