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.IntrinsicCandidate;
30 import jdk.internal.vm.annotation.Stable;
31
32 import java.lang.constant.Constable;
33 import java.lang.constant.DynamicConstantDesc;
34 import java.util.Optional;
35
36 import static java.lang.constant.ConstantDescs.BSM_EXPLICIT_CAST;
37 import static java.lang.constant.ConstantDescs.CD_int;
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");
241 int size = -(-128) + 127 + 1;
242
243 // Load and use the archived cache if it exists
244 CDS.initializeFromArchive(ShortCache.class);
245 if (archivedCache == null) {
246 Short[] c = new Short[size];
247 short value = -128;
248 for(int i = 0; i < size; i++) {
249 c[i] = new Short(value++);
250 }
251 archivedCache = c;
252 }
253 cache = archivedCache;
254 assert cache.length == size;
255 }
256 }
257
258 /**
259 * Returns a {@code Short} instance representing the specified
260 * {@code short} value.
261 * If a new {@code Short} instance is not required, this method
262 * should generally be used in preference to the constructor
263 * {@link #Short(short)}, as this method is likely to yield
264 * significantly better space and time performance by caching
265 * frequently requested values.
266 *
267 * This method will always cache values in the range -128 to 127,
268 * inclusive, and may cache other values outside of this range.
269 *
270 * @param s a short value.
271 * @return a {@code Short} instance representing {@code s}.
272 * @since 1.5
273 */
274 @IntrinsicCandidate
275 public static Short valueOf(short s) {
276 final int offset = 128;
277 int sAsInt = s;
278 if (sAsInt >= -128 && sAsInt <= 127) { // must cache
279 return ShortCache.cache[sAsInt + offset];
280 }
281 return new Short(s);
282 }
283
284 /**
285 * Decodes a {@code String} into a {@code Short}.
286 * Accepts decimal, hexadecimal, and octal numbers given by
287 * the following grammar:
288 *
289 * <blockquote>
290 * <dl>
291 * <dt><i>DecodableString:</i>
292 * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
293 * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
294 * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
295 * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
296 * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
297 *
298 * <dt><i>Sign:</i>
299 * <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.IntrinsicCandidate;
32 import jdk.internal.vm.annotation.Stable;
33
34 import java.lang.constant.Constable;
35 import java.lang.constant.DynamicConstantDesc;
36 import java.util.Optional;
37
38 import static java.lang.constant.ConstantDescs.BSM_EXPLICIT_CAST;
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 {@linkplain #equals(Object) equal}
55 * as interchangeable and should not use instances for synchronization, mutexes, or
56 * with {@linkplain java.lang.ref.Reference object references}.
57 *
58 * <div class="preview-block">
59 * <div class="preview-comment">
60 * When preview features are enabled, {@code Short} is a {@linkplain Class#isValue value class}.
61 * Use of value class instances for synchronization, mutexes, or with
62 * {@linkplain java.lang.ref.Reference object references} result in
63 * {@link IdentityException}.
64 * </div>
65 * </div>
66 *
67 * @see java.lang.Number
68 * @since 1.1
69 */
70 @jdk.internal.MigratedValueClass
71 @jdk.internal.ValueBased
72 public final class Short extends Number implements Comparable<Short>, Constable {
73
74 /**
75 * A constant holding the minimum value a {@code short} can
76 * have, -2<sup>15</sup>.
77 */
78 public static final short MIN_VALUE = -32768;
79
80 /**
81 * A constant holding the maximum value a {@code short} can
82 * have, 2<sup>15</sup>-1.
83 */
84 public static final short MAX_VALUE = 32767;
85
86 /**
87 * The {@code Class} instance representing the primitive type
88 * {@code short}.
89 */
90 public static final Class<Short> TYPE = Class.getPrimitiveClass("short");
251 int size = -(-128) + 127 + 1;
252
253 // Load and use the archived cache if it exists
254 CDS.initializeFromArchive(ShortCache.class);
255 if (archivedCache == null) {
256 Short[] c = new Short[size];
257 short value = -128;
258 for(int i = 0; i < size; i++) {
259 c[i] = new Short(value++);
260 }
261 archivedCache = c;
262 }
263 cache = archivedCache;
264 assert cache.length == size;
265 }
266 }
267
268 /**
269 * Returns a {@code Short} instance representing the specified
270 * {@code short} value.
271 * <div class="preview-block">
272 * <div class="preview-comment">
273 * <p>
274 * - When preview features are NOT enabled, {@code Short} is an identity class.
275 * If a new {@code Short} instance is not required, this method
276 * should generally be used in preference to the constructor
277 * {@link #Short(short)}, as this method is likely to yield
278 * significantly better space and time performance by caching
279 * frequently requested values.
280 * This method will always cache values in the range -128 to 127,
281 * inclusive, and may cache other values outside of this range.
282 * </p>
283 * <p>
284 * - When preview features are enabled, {@code Short} is a {@linkplain Class#isValue value class}.
285 * The {@code valueOf} behavior is the same as invoking the constructor,
286 * whether cached or not.
287 * </p>
288 * </div>
289 * </div>
290 *
291 * @param s a short value.
292 * @return a {@code Short} instance representing {@code s}.
293 * @since 1.5
294 */
295 @IntrinsicCandidate
296 @DeserializeConstructor
297 public static Short valueOf(short s) {
298 if (!PreviewFeatures.isEnabled()) {
299 final int offset = 128;
300 int sAsInt = s;
301 if (sAsInt >= -128 && sAsInt <= 127) { // must cache
302 return ShortCache.cache[sAsInt + offset];
303 }
304 }
305 return new Short(s);
306 }
307
308 /**
309 * Decodes a {@code String} into a {@code Short}.
310 * Accepts decimal, hexadecimal, and octal numbers given by
311 * the following grammar:
312 *
313 * <blockquote>
314 * <dl>
315 * <dt><i>DecodableString:</i>
316 * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
317 * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
318 * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
319 * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
320 * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
321 *
322 * <dt><i>Sign:</i>
323 * <dd>{@code -}
|