2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
21 * The wrapper for the primitive type {@code byte}.
25 public final class Byte extends Number implements Comparable<Byte> {
27 private static final long serialVersionUID = -7183698231559129828L;
30 * The value which the receiver represents.
32 private final byte value;
35 * The maximum {@code Byte} value, 2<sup>7</sup>-1.
37 public static final byte MAX_VALUE = (byte) 0x7F;
40 * The minimum {@code Byte} value, -2<sup>7</sup>.
42 public static final byte MIN_VALUE = (byte) 0x80;
45 * The number of bits needed to represent a {@code Byte} value in two's
50 public static final int SIZE = 8;
53 * The {@link Class} object that represents the primitive type {@code byte}.
55 @SuppressWarnings("unchecked")
56 public static final Class<Byte> TYPE
57 = (Class<Byte>) byte[].class.getComponentType();
58 // Note: This can't be set to "byte.class", since *that* is
59 // defined to be "java.lang.Byte.TYPE";
62 * Constructs a new {@code Byte} with the specified primitive byte value.
65 * the primitive byte value to store in the new instance.
67 public Byte(byte value) {
72 * Constructs a new {@code Byte} from the specified string.
75 * the string representation of a single byte value.
76 * @throws NumberFormatException
77 * if {@code string} can not be decoded into a byte value.
78 * @see #parseByte(String)
80 public Byte(String string) throws NumberFormatException {
81 this(parseByte(string));
85 * Gets the primitive value of this byte.
87 * @return this object's primitive value.
90 public byte byteValue() {
95 * Compares this object to the specified byte object to determine their
99 * the byte object to compare this object to.
100 * @return a negative value if the value of this byte is less than the value
101 * of {@code object}; 0 if the value of this byte and the value of
102 * {@code object} are equal; a positive value if the value of this
103 * byte is greater than the value of {@code object}.
104 * @see java.lang.Comparable
107 public int compareTo(Byte object) {
108 return value > object.value ? 1 : (value < object.value ? -1 : 0);
112 * Parses the specified string and returns a {@code Byte} instance if the
113 * string can be decoded into a single byte value. The string may be an
114 * optional minus sign "-" followed by a hexadecimal ("0x..." or "#..."),
115 * octal ("0..."), or decimal ("...") representation of a byte.
118 * a string representation of a single byte value.
119 * @return a {@code Byte} containing the value represented by {@code string}.
120 * @throws NumberFormatException
121 * if {@code string} can not be parsed as a byte value.
123 public static Byte decode(String string) throws NumberFormatException {
124 int intValue = Integer.decode(string).intValue();
125 byte result = (byte) intValue;
126 if (result == intValue) {
127 return valueOf(result);
129 throw new NumberFormatException();
133 public double doubleValue() {
138 * Compares this object with the specified object and indicates if they are
139 * equal. In order to be equal, {@code object} must be an instance of
140 * {@code Byte} and have the same byte value as this object.
143 * the object to compare this byte with.
144 * @return {@code true} if the specified object is equal to this
145 * {@code Byte}; {@code false} otherwise.
148 public boolean equals(Object object) {
149 return (object == this) || (object instanceof Byte)
150 && (value == ((Byte) object).value);
154 public float floatValue() {
159 public int hashCode() {
164 public int intValue() {
169 public long longValue() {
174 * Parses the specified string as a signed decimal byte value. The ASCII
175 * character \u002d ('-') is recognized as the minus sign.
178 * the string representation of a single byte value.
179 * @return the primitive byte value represented by {@code string}.
180 * @throws NumberFormatException
181 * if {@code string} is {@code null}, has a length of zero or
182 * can not be parsed as a byte value.
184 public static byte parseByte(String string) throws NumberFormatException {
185 int intValue = Integer.parseInt(string);
186 byte result = (byte) intValue;
187 if (result == intValue) {
190 throw new NumberFormatException();
194 * Parses the specified string as a signed byte value using the specified
195 * radix. The ASCII character \u002d ('-') is recognized as the minus sign.
198 * the string representation of a single byte value.
200 * the radix to use when parsing.
201 * @return the primitive byte value represented by {@code string} using
203 * @throws NumberFormatException
204 * if {@code string} is {@code null} or has a length of zero,
205 * {@code radix < Character.MIN_RADIX},
206 * {@code radix > Character.MAX_RADIX}, or if {@code string}
207 * can not be parsed as a byte value.
209 public static byte parseByte(String string, int radix)
210 throws NumberFormatException {
211 int intValue = Integer.parseInt(string, radix);
212 byte result = (byte) intValue;
213 if (result == intValue) {
216 throw new NumberFormatException();
220 public short shortValue() {
225 public String toString() {
226 return Integer.toString(value);
230 * Returns a string containing a concise, human-readable description of the
231 * specified byte value.
234 * the byte to convert to a string.
235 * @return a printable representation of {@code value}.
237 public static String toString(byte value) {
238 return Integer.toString(value);
242 * Parses the specified string as a signed decimal byte value.
245 * the string representation of a single byte value.
246 * @return a {@code Byte} instance containing the byte value represented by
248 * @throws NumberFormatException
249 * if {@code string} is {@code null}, has a length of zero or
250 * can not be parsed as a byte value.
251 * @see #parseByte(String)
253 public static Byte valueOf(String string) throws NumberFormatException {
254 return valueOf(parseByte(string));
258 * Parses the specified string as a signed byte value using the specified
262 * the string representation of a single byte value.
264 * the radix to use when parsing.
265 * @return a {@code Byte} instance containing the byte value represented by
266 * {@code string} using {@code radix}.
267 * @throws NumberFormatException
268 * if {@code string} is {@code null} or has a length of zero,
269 * {@code radix < Character.MIN_RADIX},
270 * {@code radix > Character.MAX_RADIX}, or if {@code string}
271 * can not be parsed as a byte value.
272 * @see #parseByte(String, int)
274 public static Byte valueOf(String string, int radix)
275 throws NumberFormatException {
276 return valueOf(parseByte(string, radix));
280 * Returns a {@code Byte} instance for the specified byte value.
282 * If it is not necessary to get a new {@code Byte} instance, it is
283 * recommended to use this method instead of the constructor, since it
284 * maintains a cache of instances which may result in better performance.
287 * the byte value to store in the instance.
288 * @return a {@code Byte} instance containing {@code b}.
291 public static Byte valueOf(byte b) {
292 return VALUES[b + 128];
296 * A cache of instances used by {@link Byte#valueOf(byte)} and auto-boxing
298 private static final Byte[] VALUES = new Byte[256];
301 for (int i = -128; i < 128; i++) {
302 VALUES[i + 128] = new Byte((byte) i);