001 /*
002 * Copyright 1996-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.lang;
027
028 /**
029 *
030 * The {@code Byte} class wraps a value of primitive type {@code byte}
031 * in an object. An object of type {@code Byte} contains a single
032 * field whose type is {@code byte}.
033 *
034 * <p>In addition, this class provides several methods for converting
035 * a {@code byte} to a {@code String} and a {@code String} to a {@code
036 * byte}, as well as other constants and methods useful when dealing
037 * with a {@code byte}.
038 *
039 * @author Nakul Saraiya
040 * @author Joseph D. Darcy
041 * @version 1.49, 05/05/07
042 * @see java.lang.Number
043 * @since JDK1.1
044 */
045 public final class Byte extends Number implements Comparable<Byte> {
046
047 /**
048 * A constant holding the minimum value a {@code byte} can
049 * have, -2<sup>7</sup>.
050 */
051 public static final byte MIN_VALUE = -128;
052
053 /**
054 * A constant holding the maximum value a {@code byte} can
055 * have, 2<sup>7</sup>-1.
056 */
057 public static final byte MAX_VALUE = 127;
058
059 /**
060 * The {@code Class} instance representing the primitive type
061 * {@code byte}.
062 */
063 public static final Class<Byte> TYPE = (Class<Byte>) Class
064 .getPrimitiveClass("byte");
065
066 /**
067 * Returns a new {@code String} object representing the
068 * specified {@code byte}. The radix is assumed to be 10.
069 *
070 * @param b the {@code byte} to be converted
071 * @return the string representation of the specified {@code byte}
072 * @see java.lang.Integer#toString(int)
073 */
074 public static String toString(byte b) {
075 return Integer.toString((int) b, 10);
076 }
077
078 private static class ByteCache {
079 private ByteCache() {
080 }
081
082 static final Byte cache[] = new Byte[-(-128) + 127 + 1];
083
084 static {
085 for (int i = 0; i < cache.length; i++)
086 cache[i] = new Byte((byte) (i - 128));
087 }
088 }
089
090 /**
091 * Returns a {@code Byte} instance representing the specified
092 * {@code byte} value.
093 * If a new {@code Byte} instance is not required, this method
094 * should generally be used in preference to the constructor
095 * {@link #Byte(byte)}, as this method is likely to yield
096 * significantly better space and time performance by caching
097 * frequently requested values.
098 *
099 * @param b a byte value.
100 * @return a {@code Byte} instance representing {@code b}.
101 * @since 1.5
102 */
103 public static Byte valueOf(byte b) {
104 final int offset = 128;
105 return ByteCache.cache[(int) b + offset];
106 }
107
108 /**
109 * Parses the string argument as a signed {@code byte} in the
110 * radix specified by the second argument. The characters in the
111 * string must all be digits, of the specified radix (as
112 * determined by whether {@link java.lang.Character#digit(char,
113 * int)} returns a nonnegative value) except that the first
114 * character may be an ASCII minus sign {@code '-'}
115 * (<code>'\u002D'</code>) to indicate a negative value or an
116 * ASCII plus sign {@code '+'} (<code>'\u002B'</code>) to
117 * indicate a positive value. The resulting {@code byte} value is
118 * returned.
119 *
120 * <p>An exception of type {@code NumberFormatException} is
121 * thrown if any of the following situations occurs:
122 * <ul>
123 * <li> The first argument is {@code null} or is a string of
124 * length zero.
125 *
126 * <li> The radix is either smaller than {@link
127 * java.lang.Character#MIN_RADIX} or larger than {@link
128 * java.lang.Character#MAX_RADIX}.
129 *
130 * <li> Any character of the string is not a digit of the
131 * specified radix, except that the first character may be a minus
132 * sign {@code '-'} (<code>'\u002D'</code>) or plus sign
133 * {@code '+'} (<code>'\u002B'</code>) provided that the
134 * string is longer than length 1.
135 *
136 * <li> The value represented by the string is not a value of type
137 * {@code byte}.
138 * </ul>
139 *
140 * @param s the {@code String} containing the
141 * {@code byte}
142 * representation to be parsed
143 * @param radix the radix to be used while parsing {@code s}
144 * @return the {@code byte} value represented by the string
145 * argument in the specified radix
146 * @throws NumberFormatException If the string does
147 * not contain a parsable {@code byte}.
148 */
149 public static byte parseByte(String s, int radix)
150 throws NumberFormatException {
151 int i = Integer.parseInt(s, radix);
152 if (i < MIN_VALUE || i > MAX_VALUE)
153 throw new NumberFormatException(
154 "Value out of range. Value:\"" + s + "\" Radix:"
155 + radix);
156 return (byte) i;
157 }
158
159 /**
160 * Parses the string argument as a signed decimal {@code
161 * byte}. The characters in the string must all be decimal digits,
162 * except that the first character may be an ASCII minus sign
163 * {@code '-'} (<code>'\u002D'</code>) to indicate a negative
164 * value or an ASCII plus sign {@code '+'}
165 * (<code>'\u002B'</code>) to indicate a positive value. The
166 * resulting {@code byte} value is returned, exactly as if the
167 * argument and the radix 10 were given as arguments to the {@link
168 * #parseByte(java.lang.String, int)} method.
169 *
170 * @param s a {@code String} containing the
171 * {@code byte} representation to be parsed
172 * @return the {@code byte} value represented by the
173 * argument in decimal
174 * @throws NumberFormatException if the string does not
175 * contain a parsable {@code byte}.
176 */
177 public static byte parseByte(String s) throws NumberFormatException {
178 return parseByte(s, 10);
179 }
180
181 /**
182 * Returns a {@code Byte} object holding the value
183 * extracted from the specified {@code String} when parsed
184 * with the radix given by the second argument. The first argument
185 * is interpreted as representing a signed {@code byte} in
186 * the radix specified by the second argument, exactly as if the
187 * argument were given to the {@link #parseByte(java.lang.String,
188 * int)} method. The result is a {@code Byte} object that
189 * represents the {@code byte} value specified by the string.
190 *
191 * <p> In other words, this method returns a {@code Byte} object
192 * equal to the value of:
193 *
194 * <blockquote>
195 * {@code new Byte(Byte.parseByte(s, radix))}
196 * </blockquote>
197 *
198 * @param s the string to be parsed
199 * @param radix the radix to be used in interpreting {@code s}
200 * @return a {@code Byte} object holding the value
201 * represented by the string argument in the
202 * specified radix.
203 * @throws NumberFormatException If the {@code String} does
204 * not contain a parsable {@code byte}.
205 */
206 public static Byte valueOf(String s, int radix)
207 throws NumberFormatException {
208 return new Byte(parseByte(s, radix));
209 }
210
211 /**
212 * Returns a {@code Byte} object holding the value
213 * given by the specified {@code String}. The argument is
214 * interpreted as representing a signed decimal {@code byte},
215 * exactly as if the argument were given to the {@link
216 * #parseByte(java.lang.String)} method. The result is a
217 * {@code Byte} object that represents the {@code byte}
218 * value specified by the string.
219 *
220 * <p> In other words, this method returns a {@code Byte} object
221 * equal to the value of:
222 *
223 * <blockquote>
224 * {@code new Byte(Byte.parseByte(s))}
225 * </blockquote>
226 *
227 * @param s the string to be parsed
228 * @return a {@code Byte} object holding the value
229 * represented by the string argument
230 * @throws NumberFormatException If the {@code String} does
231 * not contain a parsable {@code byte}.
232 */
233 public static Byte valueOf(String s) throws NumberFormatException {
234 return valueOf(s, 10);
235 }
236
237 /**
238 * Decodes a {@code String} into a {@code Byte}.
239 * Accepts decimal, hexadecimal, and octal numbers given by
240 * the following grammar:
241 *
242 * <blockquote>
243 * <dl>
244 * <dt><i>DecodableString:</i>
245 * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
246 * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
247 * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
248 * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
249 * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
250 * <p>
251 * <dt><i>Sign:</i>
252 * <dd>{@code -}
253 * <dd>{@code +}
254 * </dl>
255 * </blockquote>
256 *
257 * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>
258 * are defined in <a href="http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#48282">§3.10.1</a>
259 * of the <a href="http://java.sun.com/docs/books/jls/html/">Java
260 * Language Specification</a>.
261 *
262 * <p>The sequence of characters following an optional
263 * sign and/or radix specifier ("{@code 0x}", "{@code 0X}",
264 * "{@code #}", or leading zero) is parsed as by the {@code
265 * Byte.parseByte} method with the indicated radix (10, 16, or 8).
266 * This sequence of characters must represent a positive value or
267 * a {@link NumberFormatException} will be thrown. The result is
268 * negated if first character of the specified {@code String} is
269 * the minus sign. No whitespace characters are permitted in the
270 * {@code String}.
271 *
272 * @param nm the {@code String} to decode.
273 * @return a {@code Byte} object holding the {@code byte}
274 * value represented by {@code nm}
275 * @throws NumberFormatException if the {@code String} does not
276 * contain a parsable {@code byte}.
277 * @see java.lang.Byte#parseByte(java.lang.String, int)
278 */
279 public static Byte decode(String nm) throws NumberFormatException {
280 int i = Integer.decode(nm);
281 if (i < MIN_VALUE || i > MAX_VALUE)
282 throw new NumberFormatException("Value " + i
283 + " out of range from input " + nm);
284 return (byte) i;
285 }
286
287 /**
288 * The value of the {@code Byte}.
289 *
290 * @serial
291 */
292 private final byte value;
293
294 /**
295 * Constructs a newly allocated {@code Byte} object that
296 * represents the specified {@code byte} value.
297 *
298 * @param value the value to be represented by the
299 * {@code Byte}.
300 */
301 public Byte(byte value) {
302 this .value = value;
303 }
304
305 /**
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 public byte byteValue() {
327 return value;
328 }
329
330 /**
331 * Returns the value of this {@code Byte} as a
332 * {@code short}.
333 */
334 public short shortValue() {
335 return (short) value;
336 }
337
338 /**
339 * Returns the value of this {@code Byte} as an
340 * {@code int}.
341 */
342 public int intValue() {
343 return (int) value;
344 }
345
346 /**
347 * Returns the value of this {@code Byte} as a
348 * {@code long}.
349 */
350 public long longValue() {
351 return (long) value;
352 }
353
354 /**
355 * Returns the value of this {@code Byte} as a
356 * {@code float}.
357 */
358 public float floatValue() {
359 return (float) value;
360 }
361
362 /**
363 * Returns the value of this {@code Byte} as a
364 * {@code double}.
365 */
366 public double doubleValue() {
367 return (double) value;
368 }
369
370 /**
371 * Returns a {@code String} object representing this
372 * {@code Byte}'s value. The value is converted to signed
373 * decimal representation and returned as a string, exactly as if
374 * the {@code byte} value were given as an argument to the
375 * {@link java.lang.Byte#toString(byte)} method.
376 *
377 * @return a string representation of the value of this object in
378 * base 10.
379 */
380 public String toString() {
381 return String.valueOf((int) value);
382 }
383
384 /**
385 * Returns a hash code for this {@code Byte}.
386 */
387 public int hashCode() {
388 return (int) value;
389 }
390
391 /**
392 * Compares this object to the specified object. The result is
393 * {@code true} if and only if the argument is not
394 * {@code null} and is a {@code Byte} object that
395 * contains the same {@code byte} value as this object.
396 *
397 * @param obj the object to compare with
398 * @return {@code true} if the objects are the same;
399 * {@code false} otherwise.
400 */
401 public boolean equals(Object obj) {
402 if (obj instanceof Byte) {
403 return value == ((Byte) obj).byteValue();
404 }
405 return false;
406 }
407
408 /**
409 * Compares two {@code Byte} objects numerically.
410 *
411 * @param anotherByte the {@code Byte} to be compared.
412 * @return the value {@code 0} if this {@code Byte} is
413 * equal to the argument {@code Byte}; a value less than
414 * {@code 0} if this {@code Byte} is numerically less
415 * than the argument {@code Byte}; and a value greater than
416 * {@code 0} if this {@code Byte} is numerically
417 * greater than the argument {@code Byte} (signed
418 * comparison).
419 * @since 1.2
420 */
421 public int compareTo(Byte anotherByte) {
422 return this .value - anotherByte.value;
423 }
424
425 /**
426 * The number of bits used to represent a {@code byte} value in two's
427 * complement binary form.
428 *
429 * @since 1.5
430 */
431 public static final int SIZE = 8;
432
433 /** use serialVersionUID from JDK 1.1. for interoperability */
434 private static final long serialVersionUID = -7183698231559129828L;
435 }
|