< prev index next >

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

Print this page

  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");

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      *
290      * <blockquote>
291      * <dl>
292      * <dt><i>DecodableString:</i>
293      * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
294      * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
295      * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
296      * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
297      * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
298      *
299      * <dt><i>Sign:</i>
300      * <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");

233      * Returns an {@link Optional} containing the nominal descriptor for this
234      * instance.
235      *
236      * @return an {@link Optional} describing the {@linkplain Short} instance
237      * @since 15
238      */
239     @Override
240     public Optional<DynamicConstantDesc<Short>> describeConstable() {
241         return Optional.of(DynamicConstantDesc.ofNamed(BSM_EXPLICIT_CAST, DEFAULT_NAME, CD_short, intValue()));
242     }
243 
244     @AOTSafeClassInitializer
245     private static final class ShortCache {
246         private ShortCache() {}
247 
248         @Stable
249         static final Short[] cache;
250         static Short[] archivedCache;
251 
252         static {
253             if (!PreviewFeatures.isEnabled()) {
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 = new Short[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             } else {
269                 cache = null;
270                 assert archivedCache == null;
271             }


272         }
273     }
274 
275     /**
276      * Returns a {@code Short} instance representing the specified
277      * {@code short} value.
278      * <div class="preview-block">
279      *      <div class="preview-comment">
280      *          <p>
281      *              - When preview features are NOT enabled, {@code Short} is an identity class.
282      *              If a new {@code Short} instance is not required, this method
283      *              should generally be used in preference to the constructor
284      *              {@link #Short(short)}, as this method is likely to yield
285      *              significantly better space and time performance by caching
286      *              frequently requested values.
287      *              This method will always cache values in the range -128 to 127,
288      *              inclusive, and may cache other values outside of this range.
289      *          </p>
290      *          <p>
291      *              - When preview features are enabled, {@code Short} is a {@linkplain Class#isValue value class}.
292      *              The {@code valueOf} behavior is the same as invoking the constructor,
293      *              whether cached or not.
294      *          </p>
295      *      </div>
296      * </div>
297      *
298      * @param  s a short value.
299      * @return a {@code Short} instance representing {@code s}.
300      * @since  1.5
301      */
302     @IntrinsicCandidate
303     @DeserializeConstructor
304     public static Short valueOf(short s) {
305         if (!PreviewFeatures.isEnabled()) {
306             final int offset = 128;
307             int sAsInt = s;
308             if (sAsInt >= -128 && sAsInt <= 127) { // must cache
309                 return ShortCache.cache[sAsInt + offset];
310             }
311         }
312         return new Short(s);
313     }
314 
315     /**
316      * Decodes a {@code String} into a {@code Short}.
317      * Accepts decimal, hexadecimal, and octal numbers given by
318      * the following grammar:
319      *
320      * <blockquote>
321      * <dl>
322      * <dt><i>DecodableString:</i>
323      * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
324      * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
325      * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
326      * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
327      * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
328      *
329      * <dt><i>Sign:</i>
330      * <dd>{@code -}
< prev index next >