< prev index next >

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

Print this page

  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 public final /*value*/ class Short extends Number
 74         implements Comparable<Short>, Constable {
 75 
 76     /**
 77      * A constant holding the minimum value a {@code short} can
 78      * have, -2<sup>15</sup>.
 79      */
 80     public static final short   MIN_VALUE = -32768;
 81 
 82     /**
 83      * A constant holding the maximum value a {@code short} can
 84      * have, 2<sup>15</sup>-1.
 85      */
 86     public static final short   MAX_VALUE = 32767;
 87 
 88     /**
 89      * The {@code Class} instance representing the primitive type
 90      * {@code short}.
 91      */
 92     public static final Class<Short> TYPE = Class.getPrimitiveClass("short");
 93 
 94     /**

239      */
240     @Override
241     public Optional<DynamicConstantDesc<Short>> describeConstable() {
242         return Optional.of(DynamicConstantDesc.ofNamed(BSM_EXPLICIT_CAST, DEFAULT_NAME, CD_short, intValue()));
243     }
244 
245     @AOTSafeClassInitializer
246     private static final class ShortCache {
247         private ShortCache() {}
248 
249         @Stable
250         static final Short[] cache;
251         static Short[] archivedCache;
252 
253         static {
254             int size = -(-128) + 127 + 1;
255 
256             // Load and use the archived cache if it exists
257             CDS.initializeFromArchive(ShortCache.class);
258             if (archivedCache == null) {
259                 Short[] c = newCacheArray(size);
260                 short value = -128;
261                 for(int i = 0; i < size; i++) {
262                     c[i] = new Short(value++);
263                 }
264                 archivedCache = c;
265             }
266             cache = archivedCache;
267             assert cache.length == size;
268         }
269 
270         private static Short[] newCacheArray(int size) {
271             // ValueClass.newReferenceArray requires a value class component.
272             if (PreviewFeatures.isEnabled()) {
273                 return (Short[]) ValueClass.newReferenceArray(Short.class, size);
274             }
275             return new Short[size];
276         }
277     }
278 
279     /**
280      * Returns a {@code Short} instance representing the specified
281      * {@code short} value.
282      * <div class="preview-block">
283      *      <div class="preview-comment">
284      *          <p>
285      *              - When preview features are NOT enabled, {@code Short} is an identity class.
286      *              If a new {@code Short} instance is not required, this method
287      *              should generally be used in preference to the constructor
288      *              {@link #Short(short)}, as this method is likely to yield
289      *              significantly better space and time performance by caching
290      *              frequently requested values.
291      *              This method will always cache values in the range -128 to 127,
292      *              inclusive, and may cache other values outside of this range.
293      *          </p>
294      *          <p>
295      *              - When preview features are enabled, {@code Short} is a {@linkplain Class#isValue value class}.
296      *              The {@code valueOf} behavior is the same as invoking the constructor,
297      *              whether cached or not.
298      *          </p>
299      *      </div>
300      * </div>
301      *
302      * @param  s a short value.
303      * @return a {@code Short} instance representing {@code s}.
304      * @since  1.5
305      */
306     @IntrinsicCandidate
307     public static Short valueOf(short s) {
308         final int offset = 128;
309         int sAsInt = s;
310         if (sAsInt >= -128 && sAsInt <= 127) { // must cache
311             return ShortCache.cache[sAsInt + offset];
312         }
313         return new Short(s);
314     }
315 
316     /**
317      * Decodes a {@code String} into a {@code Short}.
318      * Accepts decimal, hexadecimal, and octal numbers given by
319      * the following grammar:
320      *

366     /**
367      * The value of the {@code Short}.
368      *
369      * @serial
370      */
371     private final short value;
372 
373     /**
374      * Constructs a newly allocated {@code Short} object that
375      * represents the specified {@code short} value.
376      *
377      * @param value     the value to be represented by the
378      *                  {@code Short}.
379      *
380      * @deprecated
381      * It is rarely appropriate to use this constructor. The static factory
382      * {@link #valueOf(short)} is generally a better choice, as it is
383      * likely to yield significantly better space and time performance.
384      */
385     @Deprecated(since="9")
386     @Deserializer("value")
387     public Short(short value) {
388         this.value = value;
389     }
390 
391     /**
392      * Constructs a newly allocated {@code Short} object that
393      * represents the {@code short} value indicated by the
394      * {@code String} parameter. The string is converted to a
395      * {@code short} value in exactly the manner used by the
396      * {@code parseShort} method for radix 10.
397      *
398      * @param s the {@code String} to be converted to a
399      *          {@code Short}
400      * @throws  NumberFormatException If the {@code String}
401      *          does not contain a parsable {@code short}.
402      *
403      * @deprecated
404      * It is rarely appropriate to use this constructor.
405      * Use {@link #parseShort(String)} to convert a string to a
406      * {@code short} primitive, or use {@link #valueOf(String)}
< prev index next >