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.CD_int;
40 import static java.lang.constant.ConstantDescs.DEFAULT_NAME;
41
42 /**
43 * The {@code Byte} class is the {@linkplain
44 * java.lang##wrapperClass wrapper class} for values of the primitive
45 * type {@code byte}. An object of type {@code Byte} contains a
46 * single field whose type is {@code byte}.
47 *
48 * <p>In addition, this class provides several methods for converting
49 * a {@code byte} to a {@code String} and a {@code String} to a {@code
50 * byte}, as well as other constants and methods useful when dealing
51 * with a {@code byte}.
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
55 * {@linkplain #equals(Object) equal} as interchangeable and should not
56 * use instances for synchronization, or unpredictable behavior may
57 * occur. For example, in a future release, synchronization may fail.
58 *
59 * @see java.lang.Number
60 * @since 1.1
61 */
62 @jdk.internal.ValueBased
63 public final class Byte extends Number implements Comparable<Byte>, Constable {
64
65 /**
66 * A constant holding the minimum value a {@code byte} can
67 * have, -2<sup>7</sup>.
68 */
69 public static final byte MIN_VALUE = -128;
70
71 /**
72 * A constant holding the maximum value a {@code byte} can
73 * have, 2<sup>7</sup>-1.
74 */
75 public static final byte MAX_VALUE = 127;
76
77 /**
78 * The {@code Class} instance representing the primitive type
79 * {@code byte}.
80 */
81 public static final Class<Byte> TYPE = Class.getPrimitiveClass("byte");
116 final int size = -(-128) + 127 + 1;
117
118 // Load and use the archived cache if it exists
119 CDS.initializeFromArchive(ByteCache.class);
120 if (archivedCache == null) {
121 Byte[] c = new Byte[size];
122 byte value = (byte)-128;
123 for(int i = 0; i < size; i++) {
124 c[i] = new Byte(value++);
125 }
126 archivedCache = c;
127 }
128 cache = archivedCache;
129 assert cache.length == size;
130 }
131 }
132
133 /**
134 * Returns a {@code Byte} instance representing the specified
135 * {@code byte} value.
136 * If a new {@code Byte} instance is not required, this method
137 * should generally be used in preference to the constructor
138 * {@link #Byte(byte)}, as this method is likely to yield
139 * significantly better space and time performance since
140 * all byte values are cached.
141 *
142 * @param b a byte value.
143 * @return a {@code Byte} instance representing {@code b}.
144 * @since 1.5
145 */
146 @IntrinsicCandidate
147 public static Byte valueOf(byte b) {
148 final int offset = 128;
149 return ByteCache.cache[(int)b + offset];
150 }
151
152 /**
153 * Parses the string argument as a signed {@code byte} in the
154 * radix specified by the second argument. The characters in the
155 * string must all be digits, of the specified radix (as
156 * determined by whether {@link java.lang.Character#digit(char,
157 * int)} returns a nonnegative value) except that the first
158 * character may be an ASCII minus sign {@code '-'}
159 * ({@code '\u005Cu002D'}) to indicate a negative value or an
160 * ASCII plus sign {@code '+'} ({@code '\u005Cu002B'}) to
161 * indicate a positive value. The resulting {@code byte} value is
162 * returned.
163 *
164 * <p>An exception of type {@code NumberFormatException} is
165 * thrown if any of the following situations occurs:
166 * <ul>
167 * <li> The first argument is {@code null} or is a string of
168 * length zero.
169 *
|
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_byte;
41 import static java.lang.constant.ConstantDescs.DEFAULT_NAME;
42
43 /**
44 * The {@code Byte} class is the {@linkplain
45 * java.lang##wrapperClass wrapper class} for values of the primitive
46 * type {@code byte}. An object of type {@code Byte} contains a
47 * single field whose type is {@code byte}.
48 *
49 * <p>In addition, this class provides several methods for converting
50 * a {@code byte} to a {@code String} and a {@code String} to a {@code
51 * byte}, as well as other constants and methods useful when dealing
52 * with a {@code byte}.
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 Byte} 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 Byte extends Number implements Comparable<Byte>, Constable {
74
75 /**
76 * A constant holding the minimum value a {@code byte} can
77 * have, -2<sup>7</sup>.
78 */
79 public static final byte MIN_VALUE = -128;
80
81 /**
82 * A constant holding the maximum value a {@code byte} can
83 * have, 2<sup>7</sup>-1.
84 */
85 public static final byte MAX_VALUE = 127;
86
87 /**
88 * The {@code Class} instance representing the primitive type
89 * {@code byte}.
90 */
91 public static final Class<Byte> TYPE = Class.getPrimitiveClass("byte");
126 final int size = -(-128) + 127 + 1;
127
128 // Load and use the archived cache if it exists
129 CDS.initializeFromArchive(ByteCache.class);
130 if (archivedCache == null) {
131 Byte[] c = new Byte[size];
132 byte value = (byte)-128;
133 for(int i = 0; i < size; i++) {
134 c[i] = new Byte(value++);
135 }
136 archivedCache = c;
137 }
138 cache = archivedCache;
139 assert cache.length == size;
140 }
141 }
142
143 /**
144 * Returns a {@code Byte} instance representing the specified
145 * {@code byte} value.
146 * <div class="preview-block">
147 * <div class="preview-comment">
148 * <p>
149 * - When preview features are NOT enabled, {@code Byte} is an identity class.
150 * If a new {@code Byte} instance is not required, this method
151 * should generally be used in preference to the constructor
152 * {@link #Byte(byte)}, as this method is likely to yield
153 * significantly better space and time performance since
154 * all byte values are cached.
155 * </p>
156 * <p>
157 * - When preview features are enabled, {@code Byte} is a {@linkplain Class#isValue value class}.
158 * The {@code valueOf} behavior is the same as invoking the constructor,
159 * whether cached or not.
160 * </p>
161 * </div>
162 * </div>
163 *
164 * @param b a byte value.
165 * @return a {@code Byte} instance representing {@code b}.
166 * @since 1.5
167 */
168 @IntrinsicCandidate
169 @DeserializeConstructor
170 public static Byte valueOf(byte b) {
171 final int offset = 128;
172 return (!PreviewFeatures.isEnabled()) ? ByteCache.cache[(int)b + offset] : new Byte(b);
173 }
174
175 /**
176 * Parses the string argument as a signed {@code byte} in the
177 * radix specified by the second argument. The characters in the
178 * string must all be digits, of the specified radix (as
179 * determined by whether {@link java.lang.Character#digit(char,
180 * int)} returns a nonnegative value) except that the first
181 * character may be an ASCII minus sign {@code '-'}
182 * ({@code '\u005Cu002D'}) to indicate a negative value or an
183 * ASCII plus sign {@code '+'} ({@code '\u005Cu002B'}) to
184 * indicate a positive value. The resulting {@code byte} value is
185 * returned.
186 *
187 * <p>An exception of type {@code NumberFormatException} is
188 * thrown if any of the following situations occurs:
189 * <ul>
190 * <li> The first argument is {@code null} or is a string of
191 * length zero.
192 *
|