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