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_byte;
39 import static java.lang.constant.ConstantDescs.DEFAULT_NAME;
40
41 /**
42 * The {@code Byte} class is the {@linkplain
43 * java.lang##wrapperClass wrapper class} for values of the primitive
44 * type {@code byte}. An object of type {@code Byte} contains a
45 * single field whose type is {@code byte}.
46 *
47 * <p>In addition, this class provides several methods for converting
48 * a {@code byte} to a {@code String} and a {@code String} to a {@code
49 * byte}, as well as other constants and methods useful when dealing
50 * with a {@code byte}.
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 Byte extends Number implements Comparable<Byte>, Constable {
63
64 /**
65 * A constant holding the minimum value a {@code byte} can
66 * have, -2<sup>7</sup>.
67 */
68 public static final byte MIN_VALUE = -128;
69
70 /**
71 * A constant holding the maximum value a {@code byte} can
72 * have, 2<sup>7</sup>-1.
73 */
74 public static final byte MAX_VALUE = 127;
75
76 /**
77 * The {@code Class} instance representing the primitive type
78 * {@code byte}.
79 */
80 public static final Class<Byte> TYPE = Class.getPrimitiveClass("byte");
81
82 /**
100 */
101 @Override
102 public Optional<DynamicConstantDesc<Byte>> describeConstable() {
103 return Optional.of(DynamicConstantDesc.ofNamed(BSM_EXPLICIT_CAST, DEFAULT_NAME, CD_byte, intValue()));
104 }
105
106 @AOTSafeClassInitializer
107 private static final class ByteCache {
108 private ByteCache() {}
109
110 @Stable
111 static final Byte[] cache;
112 static Byte[] archivedCache;
113
114 static {
115 final int size = -(-128) + 127 + 1;
116
117 // Load and use the archived cache if it exists
118 CDS.initializeFromArchive(ByteCache.class);
119 if (archivedCache == null) {
120 Byte[] c = new Byte[size];
121 byte value = (byte)-128;
122 for(int i = 0; i < size; i++) {
123 c[i] = new Byte(value++);
124 }
125 archivedCache = c;
126 }
127 cache = archivedCache;
128 assert cache.length == size;
129 }
130 }
131
132 /**
133 * Returns a {@code Byte} instance representing the specified
134 * {@code byte} value.
135 * If a new {@code Byte} instance is not required, this method
136 * should generally be used in preference to the constructor
137 * {@link #Byte(byte)}, as this method is likely to yield
138 * significantly better space and time performance since
139 * all byte values are cached.
140 *
141 * @param b a byte value.
142 * @return a {@code Byte} instance representing {@code b}.
143 * @since 1.5
144 */
145 @IntrinsicCandidate
146 public static Byte valueOf(byte b) {
147 final int offset = 128;
148 return ByteCache.cache[(int)b + offset];
149 }
150
151 /**
152 * Parses the string argument as a signed {@code byte} in the
153 * radix specified by the second argument. The characters in the
154 * string must all be digits, of the specified radix (as
155 * determined by whether {@link java.lang.Character#digit(char,
156 * int)} returns a nonnegative value) except that the first
157 * character may be an ASCII minus sign {@code '-'}
158 * ({@code '\u005Cu002D'}) to indicate a negative value or an
159 * ASCII plus sign {@code '+'} ({@code '\u005Cu002B'}) to
160 * indicate a positive value. The resulting {@code byte} value is
161 * returned.
162 *
163 * <p>An exception of type {@code NumberFormatException} is
164 * thrown if any of the following situations occurs:
165 * <ul>
166 * <li> The first argument is {@code null} or is a string of
167 * length zero.
168 *
329 /**
330 * The value of the {@code Byte}.
331 *
332 * @serial
333 */
334 private final byte value;
335
336 /**
337 * Constructs a newly allocated {@code Byte} object that
338 * represents the specified {@code byte} value.
339 *
340 * @param value the value to be represented by the
341 * {@code Byte}.
342 *
343 * @deprecated
344 * It is rarely appropriate to use this constructor. The static factory
345 * {@link #valueOf(byte)} is generally a better choice, as it is
346 * likely to yield significantly better space and time performance.
347 */
348 @Deprecated(since="9")
349 public Byte(byte value) {
350 this.value = value;
351 }
352
353 /**
354 * Constructs a newly allocated {@code Byte} object that
355 * represents the {@code byte} value indicated by the
356 * {@code String} parameter. The string is converted to a
357 * {@code byte} value in exactly the manner used by the
358 * {@code parseByte} method for radix 10.
359 *
360 * @param s the {@code String} to be converted to a
361 * {@code Byte}
362 * @throws NumberFormatException if the {@code String}
363 * does not contain a parsable {@code byte}.
364 *
365 * @deprecated
366 * It is rarely appropriate to use this constructor.
367 * Use {@link #parseByte(String)} to convert a string to a
368 * {@code byte} 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_byte;
42 import static java.lang.constant.ConstantDescs.DEFAULT_NAME;
43
44 /**
45 * The {@code Byte} class is the {@linkplain
46 * java.lang##wrapperClass wrapper class} for values of the primitive
47 * type {@code byte}. An object of type {@code Byte} contains a
48 * single field whose type is {@code byte}.
49 *
50 * <p>In addition, this class provides several methods for converting
51 * a {@code byte} to a {@code String} and a {@code String} to a {@code
52 * byte}, as well as other constants and methods useful when dealing
53 * with a {@code byte}.
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, mutexes, 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 Byte} is a {@linkplain Class#isValue value class}.
63 * Use of value class instances for synchronization, mutexes, 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.MigratedValueClass
73 @jdk.internal.ValueBased
74 public final /*value*/ class Byte extends Number
75 implements Comparable<Byte>, Constable {
76
77 /**
78 * A constant holding the minimum value a {@code byte} can
79 * have, -2<sup>7</sup>.
80 */
81 public static final byte MIN_VALUE = -128;
82
83 /**
84 * A constant holding the maximum value a {@code byte} can
85 * have, 2<sup>7</sup>-1.
86 */
87 public static final byte MAX_VALUE = 127;
88
89 /**
90 * The {@code Class} instance representing the primitive type
91 * {@code byte}.
92 */
93 public static final Class<Byte> TYPE = Class.getPrimitiveClass("byte");
94
95 /**
113 */
114 @Override
115 public Optional<DynamicConstantDesc<Byte>> describeConstable() {
116 return Optional.of(DynamicConstantDesc.ofNamed(BSM_EXPLICIT_CAST, DEFAULT_NAME, CD_byte, intValue()));
117 }
118
119 @AOTSafeClassInitializer
120 private static final class ByteCache {
121 private ByteCache() {}
122
123 @Stable
124 static final Byte[] cache;
125 static Byte[] archivedCache;
126
127 static {
128 final int size = -(-128) + 127 + 1;
129
130 // Load and use the archived cache if it exists
131 CDS.initializeFromArchive(ByteCache.class);
132 if (archivedCache == null) {
133 Byte[] c = newCacheArray(size);
134 byte value = (byte)-128;
135 for(int i = 0; i < size; i++) {
136 c[i] = new Byte(value++);
137 }
138 archivedCache = c;
139 }
140 cache = archivedCache;
141 assert cache.length == size;
142 }
143
144 private static Byte[] newCacheArray(int size) {
145 // ValueClass.newReferenceArray requires a value class component.
146 if (PreviewFeatures.isEnabled()) {
147 return (Byte[]) ValueClass.newReferenceArray(Byte.class, size);
148 }
149 return new Byte[size];
150 }
151 }
152
153 /**
154 * Returns a {@code Byte} instance representing the specified
155 * {@code byte} value.
156 * <div class="preview-block">
157 * <div class="preview-comment">
158 * <p>
159 * - When preview features are NOT enabled, {@code Byte} is an identity class.
160 * If a new {@code Byte} instance is not required, this method
161 * should generally be used in preference to the constructor
162 * {@link #Byte(byte)}, as this method is likely to yield
163 * significantly better space and time performance since
164 * all byte values are cached.
165 * </p>
166 * <p>
167 * - When preview features are enabled, {@code Byte} is a {@linkplain Class#isValue value class}.
168 * The {@code valueOf} behavior is the same as invoking the constructor,
169 * whether cached or not.
170 * </p>
171 * </div>
172 * </div>
173 *
174 * @param b a byte value.
175 * @return a {@code Byte} instance representing {@code b}.
176 * @since 1.5
177 */
178 @IntrinsicCandidate
179 public static Byte valueOf(byte b) {
180 final int offset = 128;
181 return ByteCache.cache[(int) b + offset];
182 }
183
184 /**
185 * Parses the string argument as a signed {@code byte} in the
186 * radix specified by the second argument. The characters in the
187 * string must all be digits, of the specified radix (as
188 * determined by whether {@link java.lang.Character#digit(char,
189 * int)} returns a nonnegative value) except that the first
190 * character may be an ASCII minus sign {@code '-'}
191 * ({@code '\u005Cu002D'}) to indicate a negative value or an
192 * ASCII plus sign {@code '+'} ({@code '\u005Cu002B'}) to
193 * indicate a positive value. The resulting {@code byte} value is
194 * returned.
195 *
196 * <p>An exception of type {@code NumberFormatException} is
197 * thrown if any of the following situations occurs:
198 * <ul>
199 * <li> The first argument is {@code null} or is a string of
200 * length zero.
201 *
362 /**
363 * The value of the {@code Byte}.
364 *
365 * @serial
366 */
367 private final byte value;
368
369 /**
370 * Constructs a newly allocated {@code Byte} object that
371 * represents the specified {@code byte} value.
372 *
373 * @param value the value to be represented by the
374 * {@code Byte}.
375 *
376 * @deprecated
377 * It is rarely appropriate to use this constructor. The static factory
378 * {@link #valueOf(byte)} is generally a better choice, as it is
379 * likely to yield significantly better space and time performance.
380 */
381 @Deprecated(since="9")
382 @Deserializer("value")
383 public Byte(byte value) {
384 this.value = value;
385 }
386
387 /**
388 * Constructs a newly allocated {@code Byte} object that
389 * represents the {@code byte} value indicated by the
390 * {@code String} parameter. The string is converted to a
391 * {@code byte} value in exactly the manner used by the
392 * {@code parseByte} method for radix 10.
393 *
394 * @param s the {@code String} to be converted to a
395 * {@code Byte}
396 * @throws NumberFormatException if the {@code String}
397 * does not contain a parsable {@code byte}.
398 *
399 * @deprecated
400 * It is rarely appropriate to use this constructor.
401 * Use {@link #parseByte(String)} to convert a string to a
402 * {@code byte} primitive, or use {@link #valueOf(String)}
|