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 /**
86 * @param b the {@code byte} to be converted
87 * @return the string representation of the specified {@code byte}
88 * @see java.lang.Integer#toString(int)
89 */
90 public static String toString(byte b) {
91 return Integer.toString(b);
92 }
93
94 /**
95 * Returns an {@link Optional} containing the nominal descriptor for this
96 * instance.
97 *
98 * @return an {@link Optional} describing the {@linkplain Byte} instance
99 * @since 15
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 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 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.ValueBased
73 // See doc/value-class-preview.md for an overview of value class generation
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 /**
99 * @param b the {@code byte} to be converted
100 * @return the string representation of the specified {@code byte}
101 * @see java.lang.Integer#toString(int)
102 */
103 public static String toString(byte b) {
104 return Integer.toString(b);
105 }
106
107 /**
108 * Returns an {@link Optional} containing the nominal descriptor for this
109 * instance.
110 *
111 * @return an {@link Optional} describing the {@linkplain Byte} instance
112 * @since 15
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 // When preview features are enabled, the cache does not affect object
120 // equality == semantics, but exists for performance.
121 // See doc/value-class-preview.md "Wrapper Class Caches" section.
122 @AOTSafeClassInitializer
123 private static final class ByteCache {
124 private ByteCache() {}
125
126 @Stable
127 static final Byte[] cache;
128 static Byte[] archivedCache;
129
130 static {
131 final int size = -(-128) + 127 + 1;
132
133 // Load and use the archived cache if it exists
134 CDS.initializeFromArchive(ByteCache.class);
135 if (archivedCache == null) {
136 Byte[] c = newCacheArray(size);
137 byte value = (byte)-128;
138 for(int i = 0; i < size; i++) {
139 c[i] = new Byte(value++);
140 }
141 archivedCache = c;
142 }
143 cache = archivedCache;
144 assert cache.length == size;
145 }
146
147 private static Byte[] newCacheArray(int size) {
148 // ValueClass.newReferenceArray requires a value class component.
149 if (PreviewFeatures.isEnabled()) {
150 return (Byte[]) ValueClass.newReferenceArray(Byte.class, size);
151 }
152 return new Byte[size];
153 }
154 }
155
156 /**
157 * Returns a {@code Byte} instance representing the specified
158 * {@code byte} value.
159 * <div class="preview-block">
160 * <div class="preview-comment">
161 * <p>
162 * - When preview features are NOT enabled, {@code Byte} is an identity class.
163 * If a new {@code Byte} instance is not required, this method
164 * should generally be used in preference to the constructor
165 * {@link #Byte(byte)}, as this method is likely to yield
166 * significantly better space and time performance since
167 * all byte values are cached.
168 * </p>
169 * <p>
170 * - When preview features are enabled, {@code Byte} is a {@linkplain Class#isValue value class}.
171 * The {@code valueOf} behavior is the same as invoking the constructor,
172 * whether cached or not.
173 * </p>
174 * </div>
175 * </div>
176 *
177 * @param b a byte value.
178 * @return a {@code Byte} instance representing {@code b}.
179 * @since 1.5
180 */
181 @IntrinsicCandidate
182 public static Byte valueOf(byte b) {
183 final int offset = 128;
184 return ByteCache.cache[(int) b + offset];
185 }
186
187 /**
188 * Parses the string argument as a signed {@code byte} in the
189 * radix specified by the second argument. The characters in the
190 * string must all be digits, of the specified radix (as
191 * determined by whether {@link java.lang.Character#digit(char,
192 * int)} returns a nonnegative value) except that the first
193 * character may be an ASCII minus sign {@code '-'}
194 * ({@code '\u005Cu002D'}) to indicate a negative value or an
195 * ASCII plus sign {@code '+'} ({@code '\u005Cu002B'}) to
196 * indicate a positive value. The resulting {@code byte} value is
197 * returned.
198 *
199 * <p>An exception of type {@code NumberFormatException} is
200 * thrown if any of the following situations occurs:
201 * <ul>
202 * <li> The first argument is {@code null} or is a string of
203 * length zero.
204 *
365 /**
366 * The value of the {@code Byte}.
367 *
368 * @serial
369 */
370 private final byte value;
371
372 /**
373 * Constructs a newly allocated {@code Byte} object that
374 * represents the specified {@code byte} value.
375 *
376 * @param value the value to be represented by the
377 * {@code Byte}.
378 *
379 * @deprecated
380 * It is rarely appropriate to use this constructor. The static factory
381 * {@link #valueOf(byte)} is generally a better choice, as it is
382 * likely to yield significantly better space and time performance.
383 */
384 @Deprecated(since="9")
385 @Deserializer("value")
386 public Byte(byte value) {
387 this.value = value;
388 }
389
390 /**
391 * Constructs a newly allocated {@code Byte} object that
392 * represents the {@code byte} value indicated by the
393 * {@code String} parameter. The string is converted to a
394 * {@code byte} value in exactly the manner used by the
395 * {@code parseByte} method for radix 10.
396 *
397 * @param s the {@code String} to be converted to a
398 * {@code Byte}
399 * @throws NumberFormatException if the {@code String}
400 * does not contain a parsable {@code byte}.
401 *
402 * @deprecated
403 * It is rarely appropriate to use this constructor.
404 * Use {@link #parseByte(String)} to convert a string to a
405 * {@code byte} primitive, or use {@link #valueOf(String)}
|