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 /**
29 *
30 * The {@code Byte} class wraps a value of primitive type {@code byte}
31 * in an object. An object of type {@code Byte} contains a single
32 * field whose type is {@code byte}.
33 *
34 * <p>In addition, this class provides several methods for converting
35 * a {@code byte} to a {@code String} and a {@code String} to a {@code
36 * byte}, as well as other constants and methods useful when dealing
37 * with a {@code byte}.
38 *
39 * @author Nakul Saraiya
40 * @author Joseph D. Darcy
41 * @see java.lang.Number
42 * @since 1.1
43 */
44 public final class Byte extends Number implements Comparable<Byte> {
45
46 /**
47 * A constant holding the minimum value a {@code byte} can
81
82 static {
83 for(int i = 0; i < cache.length; i++)
84 cache[i] = new Byte((byte)(i - 128));
85 }
86 }
87
88 /**
89 * Returns a {@code Byte} instance representing the specified
90 * {@code byte} value.
91 * If a new {@code Byte} instance is not required, this method
92 * should generally be used in preference to the constructor
93 * {@link #Byte(byte)}, as this method is likely to yield
94 * significantly better space and time performance since
95 * all byte values are cached.
96 *
97 * @param b a byte value.
98 * @return a {@code Byte} instance representing {@code b}.
99 * @since 1.5
100 */
101 public static Byte valueOf(byte b) {
102 final int offset = 128;
103 return ByteCache.cache[(int)b + offset];
104 }
105
106 /**
107 * Parses the string argument as a signed {@code byte} in the
108 * radix specified by the second argument. The characters in the
109 * string must all be digits, of the specified radix (as
110 * determined by whether {@link java.lang.Character#digit(char,
111 * int)} returns a nonnegative value) except that the first
112 * character may be an ASCII minus sign {@code '-'}
113 * ({@code '\u005Cu002D'}) to indicate a negative value or an
114 * ASCII plus sign {@code '+'} ({@code '\u005Cu002B'}) to
115 * indicate a positive value. The resulting {@code byte} value is
116 * returned.
117 *
118 * <p>An exception of type {@code NumberFormatException} is
119 * thrown if any of the following situations occurs:
120 * <ul>
303 * Constructs a newly allocated {@code Byte} object that
304 * represents the {@code byte} value indicated by the
305 * {@code String} parameter. The string is converted to a
306 * {@code byte} value in exactly the manner used by the
307 * {@code parseByte} method for radix 10.
308 *
309 * @param s the {@code String} to be converted to a
310 * {@code Byte}
311 * @throws NumberFormatException If the {@code String}
312 * does not contain a parsable {@code byte}.
313 * @see java.lang.Byte#parseByte(java.lang.String, int)
314 */
315 public Byte(String s) throws NumberFormatException {
316 this.value = parseByte(s, 10);
317 }
318
319 /**
320 * Returns the value of this {@code Byte} as a
321 * {@code byte}.
322 */
323 public byte byteValue() {
324 return value;
325 }
326
327 /**
328 * Returns the value of this {@code Byte} as a {@code short} after
329 * a widening primitive conversion.
330 * @jls 5.1.2 Widening Primitive Conversions
331 */
332 public short shortValue() {
333 return (short)value;
334 }
335
336 /**
337 * Returns the value of this {@code Byte} as an {@code int} after
338 * a widening primitive conversion.
339 * @jls 5.1.2 Widening Primitive Conversions
340 */
341 public int intValue() {
342 return (int)value;
|
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.HotSpotIntrinsicCandidate;
29
30 /**
31 *
32 * The {@code Byte} class wraps a value of primitive type {@code byte}
33 * in an object. An object of type {@code Byte} contains a single
34 * field whose type is {@code byte}.
35 *
36 * <p>In addition, this class provides several methods for converting
37 * a {@code byte} to a {@code String} and a {@code String} to a {@code
38 * byte}, as well as other constants and methods useful when dealing
39 * with a {@code byte}.
40 *
41 * @author Nakul Saraiya
42 * @author Joseph D. Darcy
43 * @see java.lang.Number
44 * @since 1.1
45 */
46 public final class Byte extends Number implements Comparable<Byte> {
47
48 /**
49 * A constant holding the minimum value a {@code byte} can
83
84 static {
85 for(int i = 0; i < cache.length; i++)
86 cache[i] = new Byte((byte)(i - 128));
87 }
88 }
89
90 /**
91 * Returns a {@code Byte} instance representing the specified
92 * {@code byte} value.
93 * If a new {@code Byte} instance is not required, this method
94 * should generally be used in preference to the constructor
95 * {@link #Byte(byte)}, as this method is likely to yield
96 * significantly better space and time performance since
97 * all byte values are cached.
98 *
99 * @param b a byte value.
100 * @return a {@code Byte} instance representing {@code b}.
101 * @since 1.5
102 */
103 @HotSpotIntrinsicCandidate
104 public static Byte valueOf(byte b) {
105 final int offset = 128;
106 return ByteCache.cache[(int)b + offset];
107 }
108
109 /**
110 * Parses the string argument as a signed {@code byte} in the
111 * radix specified by the second argument. The characters in the
112 * string must all be digits, of the specified radix (as
113 * determined by whether {@link java.lang.Character#digit(char,
114 * int)} returns a nonnegative value) except that the first
115 * character may be an ASCII minus sign {@code '-'}
116 * ({@code '\u005Cu002D'}) to indicate a negative value or an
117 * ASCII plus sign {@code '+'} ({@code '\u005Cu002B'}) to
118 * indicate a positive value. The resulting {@code byte} value is
119 * returned.
120 *
121 * <p>An exception of type {@code NumberFormatException} is
122 * thrown if any of the following situations occurs:
123 * <ul>
306 * Constructs a newly allocated {@code Byte} object that
307 * represents the {@code byte} value indicated by the
308 * {@code String} parameter. The string is converted to a
309 * {@code byte} value in exactly the manner used by the
310 * {@code parseByte} method for radix 10.
311 *
312 * @param s the {@code String} to be converted to a
313 * {@code Byte}
314 * @throws NumberFormatException If the {@code String}
315 * does not contain a parsable {@code byte}.
316 * @see java.lang.Byte#parseByte(java.lang.String, int)
317 */
318 public Byte(String s) throws NumberFormatException {
319 this.value = parseByte(s, 10);
320 }
321
322 /**
323 * Returns the value of this {@code Byte} as a
324 * {@code byte}.
325 */
326 @HotSpotIntrinsicCandidate
327 public byte byteValue() {
328 return value;
329 }
330
331 /**
332 * Returns the value of this {@code Byte} as a {@code short} after
333 * a widening primitive conversion.
334 * @jls 5.1.2 Widening Primitive Conversions
335 */
336 public short shortValue() {
337 return (short)value;
338 }
339
340 /**
341 * Returns the value of this {@code Byte} as an {@code int} after
342 * a widening primitive conversion.
343 * @jls 5.1.2 Widening Primitive Conversions
344 */
345 public int intValue() {
346 return (int)value;
|