1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. Oracle designates this 9 * particular file as subject to the "Classpath" exception as provided 10 * by Oracle in the LICENSE file that accompanied this code. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 23 * or visit www.oracle.com if you need additional information or have any 24 * questions. 25 */ 26 27 package java.lang; 28 29 /** 30 * The {@code Short} class wraps a value of primitive type {@code 31 * short} in an object. An object of type {@code Short} contains a 32 * single field whose type is {@code short}. 33 * 34 * <p>In addition, this class provides several methods for converting 35 * a {@code short} to a {@code String} and a {@code String} to a 36 * {@code short}, as well as other constants and methods useful when 37 * dealing with a {@code short}. 38 * 39 * @author Nakul Saraiya 40 * @author Joseph D. Darcy 41 * @see java.lang.Number 42 * @since JDK1.1 43 */ 44 public final class Short extends Number implements Comparable<Short> { 45 46 /** 47 * A constant holding the minimum value a {@code short} can 48 * have, -2<sup>15</sup>. 49 */ 50 public static final short MIN_VALUE = -32768; 51 52 /** 53 * A constant holding the maximum value a {@code short} can 54 * have, 2<sup>15</sup>-1. 55 */ 56 public static final short MAX_VALUE = 32767; 57 58 /** 59 * The {@code Class} instance representing the primitive type 60 * {@code short}. 61 */ 62 @SuppressWarnings("unchecked") 63 public static final Class<Short> TYPE = (Class<Short>) short[].class.getComponentType(); 64 65 /** 66 * Returns a new {@code String} object representing the 67 * specified {@code short}. The radix is assumed to be 10. 68 * 69 * @param s the {@code short} to be converted 70 * @return the string representation of the specified {@code short} 71 * @see java.lang.Integer#toString(int) 72 */ toString(short s)73 public static String toString(short s) { 74 return Integer.toString((int)s, 10); 75 } 76 77 /** 78 * Parses the string argument as a signed {@code short} in the 79 * radix specified by the second argument. The characters in the 80 * string must all be digits, of the specified radix (as 81 * determined by whether {@link java.lang.Character#digit(char, 82 * int)} returns a nonnegative value) except that the first 83 * character may be an ASCII minus sign {@code '-'} 84 * ({@code '\u005Cu002D'}) to indicate a negative value or an 85 * ASCII plus sign {@code '+'} ({@code '\u005Cu002B'}) to 86 * indicate a positive value. The resulting {@code short} value 87 * is returned. 88 * 89 * <p>An exception of type {@code NumberFormatException} is 90 * thrown if any of the following situations occurs: 91 * <ul> 92 * <li> The first argument is {@code null} or is a string of 93 * length zero. 94 * 95 * <li> The radix is either smaller than {@link 96 * java.lang.Character#MIN_RADIX} or larger than {@link 97 * java.lang.Character#MAX_RADIX}. 98 * 99 * <li> Any character of the string is not a digit of the 100 * specified radix, except that the first character may be a minus 101 * sign {@code '-'} ({@code '\u005Cu002D'}) or plus sign 102 * {@code '+'} ({@code '\u005Cu002B'}) provided that the 103 * string is longer than length 1. 104 * 105 * <li> The value represented by the string is not a value of type 106 * {@code short}. 107 * </ul> 108 * 109 * @param s the {@code String} containing the 110 * {@code short} representation to be parsed 111 * @param radix the radix to be used while parsing {@code s} 112 * @return the {@code short} represented by the string 113 * argument in the specified radix. 114 * @throws NumberFormatException If the {@code String} 115 * does not contain a parsable {@code short}. 116 */ parseShort(String s, int radix)117 public static short parseShort(String s, int radix) 118 throws NumberFormatException { 119 int i = Integer.parseInt(s, radix); 120 if (i < MIN_VALUE || i > MAX_VALUE) 121 throw new NumberFormatException( 122 "Value out of range. Value:\"" + s + "\" Radix:" + radix); 123 return (short)i; 124 } 125 126 /** 127 * Parses the string argument as a signed decimal {@code 128 * short}. The characters in the string must all be decimal 129 * digits, except that the first character may be an ASCII minus 130 * sign {@code '-'} ({@code '\u005Cu002D'}) to indicate a 131 * negative value or an ASCII plus sign {@code '+'} 132 * ({@code '\u005Cu002B'}) to indicate a positive value. The 133 * resulting {@code short} value is returned, exactly as if the 134 * argument and the radix 10 were given as arguments to the {@link 135 * #parseShort(java.lang.String, int)} method. 136 * 137 * @param s a {@code String} containing the {@code short} 138 * representation to be parsed 139 * @return the {@code short} value represented by the 140 * argument in decimal. 141 * @throws NumberFormatException If the string does not 142 * contain a parsable {@code short}. 143 */ parseShort(String s)144 public static short parseShort(String s) throws NumberFormatException { 145 return parseShort(s, 10); 146 } 147 148 /** 149 * Returns a {@code Short} object holding the value 150 * extracted from the specified {@code String} when parsed 151 * with the radix given by the second argument. The first argument 152 * is interpreted as representing a signed {@code short} in 153 * the radix specified by the second argument, exactly as if the 154 * argument were given to the {@link #parseShort(java.lang.String, 155 * int)} method. The result is a {@code Short} object that 156 * represents the {@code short} value specified by the string. 157 * 158 * <p>In other words, this method returns a {@code Short} object 159 * equal to the value of: 160 * 161 * <blockquote> 162 * {@code new Short(Short.parseShort(s, radix))} 163 * </blockquote> 164 * 165 * @param s the string to be parsed 166 * @param radix the radix to be used in interpreting {@code s} 167 * @return a {@code Short} object holding the value 168 * represented by the string argument in the 169 * specified radix. 170 * @throws NumberFormatException If the {@code String} does 171 * not contain a parsable {@code short}. 172 */ valueOf(String s, int radix)173 public static Short valueOf(String s, int radix) 174 throws NumberFormatException { 175 return valueOf(parseShort(s, radix)); 176 } 177 178 /** 179 * Returns a {@code Short} object holding the 180 * value given by the specified {@code String}. The argument 181 * is interpreted as representing a signed decimal 182 * {@code short}, exactly as if the argument were given to 183 * the {@link #parseShort(java.lang.String)} method. The result is 184 * a {@code Short} object that represents the 185 * {@code short} value specified by the string. 186 * 187 * <p>In other words, this method returns a {@code Short} object 188 * equal to the value of: 189 * 190 * <blockquote> 191 * {@code new Short(Short.parseShort(s))} 192 * </blockquote> 193 * 194 * @param s the string to be parsed 195 * @return a {@code Short} object holding the value 196 * represented by the string argument 197 * @throws NumberFormatException If the {@code String} does 198 * not contain a parsable {@code short}. 199 */ valueOf(String s)200 public static Short valueOf(String s) throws NumberFormatException { 201 return valueOf(s, 10); 202 } 203 204 private static class ShortCache { ShortCache()205 private ShortCache(){} 206 207 static final Short cache[] = new Short[-(-128) + 127 + 1]; 208 209 static { 210 for(int i = 0; i < cache.length; i++) 211 cache[i] = new Short((short)(i - 128)); 212 } 213 } 214 215 /** 216 * Returns a {@code Short} instance representing the specified 217 * {@code short} value. 218 * If a new {@code Short} instance is not required, this method 219 * should generally be used in preference to the constructor 220 * {@link #Short(short)}, as this method is likely to yield 221 * significantly better space and time performance by caching 222 * frequently requested values. 223 * 224 * This method will always cache values in the range -128 to 127, 225 * inclusive, and may cache other values outside of this range. 226 * 227 * @param s a short value. 228 * @return a {@code Short} instance representing {@code s}. 229 * @since 1.5 230 */ valueOf(short s)231 public static Short valueOf(short s) { 232 final int offset = 128; 233 int sAsInt = s; 234 if (sAsInt >= -128 && sAsInt <= 127) { // must cache 235 return ShortCache.cache[sAsInt + offset]; 236 } 237 return new Short(s); 238 } 239 240 /** 241 * Decodes a {@code String} into a {@code Short}. 242 * Accepts decimal, hexadecimal, and octal numbers given by 243 * the following grammar: 244 * 245 * <blockquote> 246 * <dl> 247 * <dt><i>DecodableString:</i> 248 * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i> 249 * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i> 250 * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i> 251 * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i> 252 * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i> 253 * 254 * <dt><i>Sign:</i> 255 * <dd>{@code -} 256 * <dd>{@code +} 257 * </dl> 258 * </blockquote> 259 * 260 * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i> 261 * are as defined in section 3.10.1 of 262 * <cite>The Java™ Language Specification</cite>, 263 * except that underscores are not accepted between digits. 264 * 265 * <p>The sequence of characters following an optional 266 * sign and/or radix specifier ("{@code 0x}", "{@code 0X}", 267 * "{@code #}", or leading zero) is parsed as by the {@code 268 * Short.parseShort} method with the indicated radix (10, 16, or 269 * 8). This sequence of characters must represent a positive 270 * value or a {@link NumberFormatException} will be thrown. The 271 * result is negated if first character of the specified {@code 272 * String} is the minus sign. No whitespace characters are 273 * permitted in the {@code String}. 274 * 275 * @param nm the {@code String} to decode. 276 * @return a {@code Short} object holding the {@code short} 277 * value represented by {@code nm} 278 * @throws NumberFormatException if the {@code String} does not 279 * contain a parsable {@code short}. 280 * @see java.lang.Short#parseShort(java.lang.String, int) 281 */ decode(String nm)282 public static Short decode(String nm) throws NumberFormatException { 283 int i = Integer.decode(nm); 284 if (i < MIN_VALUE || i > MAX_VALUE) 285 throw new NumberFormatException( 286 "Value " + i + " out of range from input " + nm); 287 return valueOf((short)i); 288 } 289 290 /** 291 * The value of the {@code Short}. 292 * 293 * @serial 294 */ 295 private final short value; 296 297 /** 298 * Constructs a newly allocated {@code Short} object that 299 * represents the specified {@code short} value. 300 * 301 * @param value the value to be represented by the 302 * {@code Short}. 303 */ Short(short value)304 public Short(short value) { 305 this.value = value; 306 } 307 308 /** 309 * Constructs a newly allocated {@code Short} object that 310 * represents the {@code short} value indicated by the 311 * {@code String} parameter. The string is converted to a 312 * {@code short} value in exactly the manner used by the 313 * {@code parseShort} method for radix 10. 314 * 315 * @param s the {@code String} to be converted to a 316 * {@code Short} 317 * @throws NumberFormatException If the {@code String} 318 * does not contain a parsable {@code short}. 319 * @see java.lang.Short#parseShort(java.lang.String, int) 320 */ Short(String s)321 public Short(String s) throws NumberFormatException { 322 this.value = parseShort(s, 10); 323 } 324 325 /** 326 * Returns the value of this {@code Short} as a {@code byte} after 327 * a narrowing primitive conversion. 328 * @jls 5.1.3 Narrowing Primitive Conversions 329 */ byteValue()330 public byte byteValue() { 331 return (byte)value; 332 } 333 334 /** 335 * Returns the value of this {@code Short} as a 336 * {@code short}. 337 */ shortValue()338 public short shortValue() { 339 return value; 340 } 341 342 /** 343 * Returns the value of this {@code Short} as an {@code int} after 344 * a widening primitive conversion. 345 * @jls 5.1.2 Widening Primitive Conversions 346 */ intValue()347 public int intValue() { 348 return (int)value; 349 } 350 351 /** 352 * Returns the value of this {@code Short} as a {@code long} after 353 * a widening primitive conversion. 354 * @jls 5.1.2 Widening Primitive Conversions 355 */ longValue()356 public long longValue() { 357 return (long)value; 358 } 359 360 /** 361 * Returns the value of this {@code Short} as a {@code float} 362 * after a widening primitive conversion. 363 * @jls 5.1.2 Widening Primitive Conversions 364 */ floatValue()365 public float floatValue() { 366 return (float)value; 367 } 368 369 /** 370 * Returns the value of this {@code Short} as a {@code double} 371 * after a widening primitive conversion. 372 * @jls 5.1.2 Widening Primitive Conversions 373 */ doubleValue()374 public double doubleValue() { 375 return (double)value; 376 } 377 378 /** 379 * Returns a {@code String} object representing this 380 * {@code Short}'s value. The value is converted to signed 381 * decimal representation and returned as a string, exactly as if 382 * the {@code short} value were given as an argument to the 383 * {@link java.lang.Short#toString(short)} method. 384 * 385 * @return a string representation of the value of this object in 386 * base 10. 387 */ toString()388 public String toString() { 389 return Integer.toString((int)value); 390 } 391 392 /** 393 * Returns a hash code for this {@code Short}; equal to the result 394 * of invoking {@code intValue()}. 395 * 396 * @return a hash code value for this {@code Short} 397 */ 398 @Override hashCode()399 public int hashCode() { 400 return Short.hashCode(value); 401 } 402 403 /** 404 * Returns a hash code for a {@code short} value; compatible with 405 * {@code Short.hashCode()}. 406 * 407 * @param value the value to hash 408 * @return a hash code value for a {@code short} value. 409 * @since 1.8 410 */ hashCode(short value)411 public static int hashCode(short value) { 412 return (int)value; 413 } 414 415 /** 416 * Compares this object to the specified object. The result is 417 * {@code true} if and only if the argument is not 418 * {@code null} and is a {@code Short} object that 419 * contains the same {@code short} value as this object. 420 * 421 * @param obj the object to compare with 422 * @return {@code true} if the objects are the same; 423 * {@code false} otherwise. 424 */ equals(Object obj)425 public boolean equals(Object obj) { 426 if (obj instanceof Short) { 427 return value == ((Short)obj).shortValue(); 428 } 429 return false; 430 } 431 432 /** 433 * Compares two {@code Short} objects numerically. 434 * 435 * @param anotherShort the {@code Short} to be compared. 436 * @return the value {@code 0} if this {@code Short} is 437 * equal to the argument {@code Short}; a value less than 438 * {@code 0} if this {@code Short} is numerically less 439 * than the argument {@code Short}; and a value greater than 440 * {@code 0} if this {@code Short} is numerically 441 * greater than the argument {@code Short} (signed 442 * comparison). 443 * @since 1.2 444 */ compareTo(Short anotherShort)445 public int compareTo(Short anotherShort) { 446 return compare(this.value, anotherShort.value); 447 } 448 449 /** 450 * Compares two {@code short} values numerically. 451 * The value returned is identical to what would be returned by: 452 * <pre> 453 * Short.valueOf(x).compareTo(Short.valueOf(y)) 454 * </pre> 455 * 456 * @param x the first {@code short} to compare 457 * @param y the second {@code short} to compare 458 * @return the value {@code 0} if {@code x == y}; 459 * a value less than {@code 0} if {@code x < y}; and 460 * a value greater than {@code 0} if {@code x > y} 461 * @since 1.7 462 */ compare(short x, short y)463 public static int compare(short x, short y) { 464 return x - y; 465 } 466 467 /** 468 * The number of bits used to represent a {@code short} value in two's 469 * complement binary form. 470 * @since 1.5 471 */ 472 public static final int SIZE = 16; 473 474 /** 475 * The number of bytes used to represent a {@code short} value in two's 476 * complement binary form. 477 * 478 * @since 1.8 479 */ 480 public static final int BYTES = SIZE / Byte.SIZE; 481 482 /** 483 * Returns the value obtained by reversing the order of the bytes in the 484 * two's complement representation of the specified {@code short} value. 485 * 486 * @param i the value whose bytes are to be reversed 487 * @return the value obtained by reversing (or, equivalently, swapping) 488 * the bytes in the specified {@code short} value. 489 * @since 1.5 490 */ reverseBytes(short i)491 public static short reverseBytes(short i) { 492 return (short) (((i & 0xFF00) >> 8) | (i << 8)); 493 } 494 495 496 /** 497 * Converts the argument to an {@code int} by an unsigned 498 * conversion. In an unsigned conversion to an {@code int}, the 499 * high-order 16 bits of the {@code int} are zero and the 500 * low-order 16 bits are equal to the bits of the {@code short} argument. 501 * 502 * Consequently, zero and positive {@code short} values are mapped 503 * to a numerically equal {@code int} value and negative {@code 504 * short} values are mapped to an {@code int} value equal to the 505 * input plus 2<sup>16</sup>. 506 * 507 * @param x the value to convert to an unsigned {@code int} 508 * @return the argument converted to {@code int} by an unsigned 509 * conversion 510 * @since 1.8 511 */ toUnsignedInt(short x)512 public static int toUnsignedInt(short x) { 513 return ((int) x) & 0xffff; 514 } 515 516 /** 517 * Converts the argument to a {@code long} by an unsigned 518 * conversion. In an unsigned conversion to a {@code long}, the 519 * high-order 48 bits of the {@code long} are zero and the 520 * low-order 16 bits are equal to the bits of the {@code short} argument. 521 * 522 * Consequently, zero and positive {@code short} values are mapped 523 * to a numerically equal {@code long} value and negative {@code 524 * short} values are mapped to a {@code long} value equal to the 525 * input plus 2<sup>16</sup>. 526 * 527 * @param x the value to convert to an unsigned {@code long} 528 * @return the argument converted to {@code long} by an unsigned 529 * conversion 530 * @since 1.8 531 */ toUnsignedLong(short x)532 public static long toUnsignedLong(short x) { 533 return ((long) x) & 0xffffL; 534 } 535 536 /** use serialVersionUID from JDK 1.1. for interoperability */ 537 private static final long serialVersionUID = 7515723908773894738L; 538 } 539