1 /* 2 * Copyright (c) 1996, 2011, 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 /** 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 JDK1.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 48 * have, -2<sup>7</sup>. 49 */ 50 public static final byte MIN_VALUE = -128; 51 52 /** 53 * A constant holding the maximum value a {@code byte} can 54 * have, 2<sup>7</sup>-1. 55 */ 56 public static final byte MAX_VALUE = 127; 57 58 /** 59 * The {@code Class} instance representing the primitive type 60 * {@code byte}. 61 */ 62 public static final Class<Byte> TYPE = (Class<Byte>) Class.getPrimitiveClass("byte"); 63 64 /** 65 * Returns a hash code for a {@code byte} value; compatible with 66 * {@code Byte.hashCode()}. 67 * 68 * @since 1.8 69 * 70 * @return a hash code value for a {@code byte} value. 71 */ 72 public static int hashCode(byte value) { 73 return (int)value; 74 } 75 76 /** 77 * Returns a new {@code String} object representing the 78 * specified {@code byte}. The radix is assumed to be 10. 79 * 80 * @param b the {@code byte} to be converted 81 * @return the string representation of the specified {@code byte} 82 * @see java.lang.Integer#toString(int) 83 */ 84 public static String toString(byte b) { 85 return Integer.toString((int)b, 10); 86 } 87 88 private static class ByteCache { 89 private ByteCache(){} 90 91 static final Byte cache[] = new Byte[-(-128) + 127 + 1]; 92 93 static { 94 for(int i = 0; i < cache.length; i++) 95 cache[i] = new Byte((byte)(i - 128)); 96 } 97 } 98 99 /** 100 * Returns a {@code Byte} instance representing the specified 101 * {@code byte} value. 102 * If a new {@code Byte} instance is not required, this method 103 * should generally be used in preference to the constructor 104 * {@link #Byte(byte)}, as this method is likely to yield 105 * significantly better space and time performance since 106 * all byte values are cached. 107 * 108 * @param b a byte value. 109 * @return a {@code Byte} instance representing {@code b}. 110 * @since 1.5 111 */ 112 public static Byte valueOf(byte b) { 113 final int offset = 128; 114 return ByteCache.cache[(int)b + offset]; 115 } 116 117 /** 118 * Parses the string argument as a signed {@code byte} in the 119 * radix specified by the second argument. The characters in the 120 * string must all be digits, of the specified radix (as 121 * determined by whether {@link java.lang.Character#digit(char, 122 * int)} returns a nonnegative value) except that the first 123 * character may be an ASCII minus sign {@code '-'} 124 * (<code>'\u002D'</code>) to indicate a negative value or an 125 * ASCII plus sign {@code '+'} (<code>'\u002B'</code>) to 126 * indicate a positive value. The resulting {@code byte} value is 127 * returned. 128 * 129 * <p>An exception of type {@code NumberFormatException} is 130 * thrown if any of the following situations occurs: 131 * <ul> 132 * <li> The first argument is {@code null} or is a string of 133 * length zero. 134 * 135 * <li> The radix is either smaller than {@link 136 * java.lang.Character#MIN_RADIX} or larger than {@link 137 * java.lang.Character#MAX_RADIX}. 138 * 139 * <li> Any character of the string is not a digit of the 140 * specified radix, except that the first character may be a minus 141 * sign {@code '-'} (<code>'\u002D'</code>) or plus sign 142 * {@code '+'} (<code>'\u002B'</code>) provided that the 143 * string is longer than length 1. 144 * 145 * <li> The value represented by the string is not a value of type 146 * {@code byte}. 147 * </ul> 148 * 149 * @param s the {@code String} containing the 150 * {@code byte} 151 * representation to be parsed 152 * @param radix the radix to be used while parsing {@code s} 153 * @return the {@code byte} value represented by the string 154 * argument in the specified radix 155 * @throws NumberFormatException If the string does 156 * not contain a parsable {@code byte}. 157 */ 158 public static byte parseByte(String s, int radix) 159 throws NumberFormatException { 160 int i = Integer.parseInt(s, radix); 161 if (i < MIN_VALUE || i > MAX_VALUE) 162 throw new NumberFormatException( 163 "Value out of range. Value:\"" + s + "\" Radix:" + radix); 164 return (byte)i; 165 } 166 167 /** 168 * Parses the string argument as a signed decimal {@code 169 * byte}. The characters in the string must all be decimal digits, 170 * except that the first character may be an ASCII minus sign 171 * {@code '-'} (<code>'\u002D'</code>) to indicate a negative 172 * value or an ASCII plus sign {@code '+'} 173 * (<code>'\u002B'</code>) to indicate a positive value. The 174 * resulting {@code byte} value is returned, exactly as if the 175 * argument and the radix 10 were given as arguments to the {@link 176 * #parseByte(java.lang.String, int)} method. 177 * 178 * @param s a {@code String} containing the 179 * {@code byte} representation to be parsed 180 * @return the {@code byte} value represented by the 181 * argument in decimal 182 * @throws NumberFormatException if the string does not 183 * contain a parsable {@code byte}. 184 */ 185 public static byte parseByte(String s) throws NumberFormatException { 186 return parseByte(s, 10); 187 } 188 189 /** 190 * Returns a {@code Byte} object holding the value 191 * extracted from the specified {@code String} when parsed 192 * with the radix given by the second argument. The first argument 193 * is interpreted as representing a signed {@code byte} in 194 * the radix specified by the second argument, exactly as if the 195 * argument were given to the {@link #parseByte(java.lang.String, 196 * int)} method. The result is a {@code Byte} object that 197 * represents the {@code byte} value specified by the string. 198 * 199 * <p> In other words, this method returns a {@code Byte} object 200 * equal to the value of: 201 * 202 * <blockquote> 203 * {@code new Byte(Byte.parseByte(s, radix))} 204 * </blockquote> 205 * 206 * @param s the string to be parsed 207 * @param radix the radix to be used in interpreting {@code s} 208 * @return a {@code Byte} object holding the value 209 * represented by the string argument in the 210 * specified radix. 211 * @throws NumberFormatException If the {@code String} does 212 * not contain a parsable {@code byte}. 213 */ 214 public static Byte valueOf(String s, int radix) 215 throws NumberFormatException { 216 return valueOf(parseByte(s, radix)); 217 } 218 219 /** 220 * Returns a {@code Byte} object holding the value 221 * given by the specified {@code String}. The argument is 222 * interpreted as representing a signed decimal {@code byte}, 223 * exactly as if the argument were given to the {@link 224 * #parseByte(java.lang.String)} method. The result is a 225 * {@code Byte} object that represents the {@code byte} 226 * value specified by the string. 227 * 228 * <p> In other words, this method returns a {@code Byte} object 229 * equal to the value of: 230 * 231 * <blockquote> 232 * {@code new Byte(Byte.parseByte(s))} 233 * </blockquote> 234 * 235 * @param s the string to be parsed 236 * @return a {@code Byte} object holding the value 237 * represented by the string argument 238 * @throws NumberFormatException If the {@code String} does 239 * not contain a parsable {@code byte}. 240 */ 241 public static Byte valueOf(String s) throws NumberFormatException { 242 return valueOf(s, 10); 243 } 244 245 /** 246 * Decodes a {@code String} into a {@code Byte}. 247 * Accepts decimal, hexadecimal, and octal numbers given by 248 * the following grammar: 249 * 250 * <blockquote> 251 * <dl> 252 * <dt><i>DecodableString:</i> 253 * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i> 254 * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i> 255 * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i> 256 * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i> 257 * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i> 258 * <p> 259 * <dt><i>Sign:</i> 260 * <dd>{@code -} 261 * <dd>{@code +} 262 * </dl> 263 * </blockquote> 264 * 265 * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i> 266 * are as defined in section 3.10.1 of 267 * <cite>The Java™ Language Specification</cite>, 268 * except that underscores are not accepted between digits. 269 * 270 * <p>The sequence of characters following an optional 271 * sign and/or radix specifier ("{@code 0x}", "{@code 0X}", 272 * "{@code #}", or leading zero) is parsed as by the {@code 273 * Byte.parseByte} method with the indicated radix (10, 16, or 8). 274 * This sequence of characters must represent a positive value or 275 * a {@link NumberFormatException} will be thrown. The result is 276 * negated if first character of the specified {@code String} is 277 * the minus sign. No whitespace characters are permitted in the 278 * {@code String}. 279 * 280 * @param nm the {@code String} to decode. 281 * @return a {@code Byte} object holding the {@code byte} 282 * value represented by {@code nm} 283 * @throws NumberFormatException if the {@code String} does not 284 * contain a parsable {@code byte}. 285 * @see java.lang.Byte#parseByte(java.lang.String, int) 286 */ 287 public static Byte decode(String nm) throws NumberFormatException { 288 int i = Integer.decode(nm); 289 if (i < MIN_VALUE || i > MAX_VALUE) 290 throw new NumberFormatException( 291 "Value " + i + " out of range from input " + nm); 292 return valueOf((byte)i); 293 } 294 295 /** 296 * The value of the {@code Byte}. 297 * 298 * @serial 299 */ 300 private final byte value; 301 302 /** 303 * Constructs a newly allocated {@code Byte} object that 304 * represents the specified {@code byte} value. 305 * 306 * @param value the value to be represented by the 307 * {@code Byte}. 308 */ 309 public Byte(byte value) { 310 this.value = value; 311 } 312 313 /** 314 * Constructs a newly allocated {@code Byte} object that 315 * represents the {@code byte} value indicated by the 316 * {@code String} parameter. The string is converted to a 317 * {@code byte} value in exactly the manner used by the 318 * {@code parseByte} method for radix 10. 319 * 320 * @param s the {@code String} to be converted to a 321 * {@code Byte} 322 * @throws NumberFormatException If the {@code String} 323 * does not contain a parsable {@code byte}. 324 * @see java.lang.Byte#parseByte(java.lang.String, int) 325 */ 326 public Byte(String s) throws NumberFormatException { 327 this.value = parseByte(s, 10); 328 } 329 330 /** 331 * Returns the value of this {@code Byte} as a 332 * {@code byte}. 333 */ 334 public byte byteValue() { 335 return value; 336 } 337 338 /** 339 * Returns the value of this {@code Byte} as a {@code short} after 340 * a widening primitive conversion. 341 * @jls 5.1.2 Widening Primitive Conversions 342 */ 343 public short shortValue() { 344 return (short)value; 345 } 346 347 /** 348 * Returns the value of this {@code Byte} as an {@code int} after 349 * a widening primitive conversion. 350 * @jls 5.1.2 Widening Primitive Conversions 351 */ 352 public int intValue() { 353 return (int)value; 354 } 355 356 /** 357 * Returns the value of this {@code Byte} as a {@code long} after 358 * a widening primitive conversion. 359 * @jls 5.1.2 Widening Primitive Conversions 360 */ 361 public long longValue() { 362 return (long)value; 363 } 364 365 /** 366 * Returns the value of this {@code Byte} as a {@code float} after 367 * a widening primitive conversion. 368 * @jls 5.1.2 Widening Primitive Conversions 369 */ 370 public float floatValue() { 371 return (float)value; 372 } 373 374 /** 375 * Returns the value of this {@code Byte} as a {@code double} 376 * after a widening primitive conversion. 377 * @jls 5.1.2 Widening Primitive Conversions 378 */ 379 public double doubleValue() { 380 return (double)value; 381 } 382 383 /** 384 * Returns a {@code String} object representing this 385 * {@code Byte}'s value. The value is converted to signed 386 * decimal representation and returned as a string, exactly as if 387 * the {@code byte} value were given as an argument to the 388 * {@link java.lang.Byte#toString(byte)} method. 389 * 390 * @return a string representation of the value of this object in 391 * base 10. 392 */ 393 public String toString() { 394 return Integer.toString((int)value); 395 } 396 397 /** 398 * Returns a hash code for this {@code Byte}; equal to the result 399 * of invoking {@code intValue()}. 400 * 401 * @return a hash code value for this {@code Byte} 402 */ 403 public int hashCode() { 404 return (int)value; 405 } 406 407 /** 408 * Compares this object to the specified object. The result is 409 * {@code true} if and only if the argument is not 410 * {@code null} and is a {@code Byte} object that 411 * contains the same {@code byte} value as this object. 412 * 413 * @param obj the object to compare with 414 * @return {@code true} if the objects are the same; 415 * {@code false} otherwise. 416 */ 417 public boolean equals(Object obj) { 418 if (obj instanceof Byte) { 419 return value == ((Byte)obj).byteValue(); 420 } 421 return false; 422 } 423 424 /** 425 * Compares two {@code Byte} objects numerically. 426 * 427 * @param anotherByte the {@code Byte} to be compared. 428 * @return the value {@code 0} if this {@code Byte} is 429 * equal to the argument {@code Byte}; a value less than 430 * {@code 0} if this {@code Byte} is numerically less 431 * than the argument {@code Byte}; and a value greater than 432 * {@code 0} if this {@code Byte} is numerically 433 * greater than the argument {@code Byte} (signed 434 * comparison). 435 * @since 1.2 436 */ 437 public int compareTo(Byte anotherByte) { 438 return compare(this.value, anotherByte.value); 439 } 440 441 /** 442 * Compares two {@code byte} values numerically. 443 * The value returned is identical to what would be returned by: 444 * <pre> 445 * Byte.valueOf(x).compareTo(Byte.valueOf(y)) 446 * </pre> 447 * 448 * @param x the first {@code byte} to compare 449 * @param y the second {@code byte} to compare 450 * @return the value {@code 0} if {@code x == y}; 451 * a value less than {@code 0} if {@code x < y}; and 452 * a value greater than {@code 0} if {@code x > y} 453 * @since 1.7 454 */ 455 public static int compare(byte x, byte y) { 456 return x - y; 457 } 458 459 /** 460 * The number of bits used to represent a {@code byte} value in two's 461 * complement binary form. 462 * 463 * @since 1.5 464 */ 465 public static final int SIZE = 8; 466 467 /** 468 * The number of bytes used to represent a {@code byte} value in two's 469 * complement binary form. 470 * 471 * @since 1.8 472 */ 473 public static final int BYTES = Byte.SIZE / Byte.SIZE; 474 475 /** use serialVersionUID from JDK 1.1. for interoperability */ 476 private static final long serialVersionUID = -7183698231559129828L; 477 }