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