首页 > 代码库 > java.lang.Long
java.lang.Long
1 package java.lang; 2 3 /** 4 * The <code>Long</code> class wraps a value of the primitive type 5 * <code>long</code> in an object. An object of type <code>Long</code> contains 6 * a single field whose type is <code>long</code>. 7 * 8 * <p> 9 * 10 * In addition, this class provides several methods for converting a 11 * <code>long</code> to a <code>String</code> and a <code>String</code> to a 12 * <code>long</code>, as well as other constants and methods useful when dealing 13 * with a <code>long</code>. 14 * 15 * <p> 16 * Implementation note: The implementations of the "bit twiddling" methods (such 17 * as {@link #highestOneBit(long) highestOneBit} and 18 * {@link #numberOfTrailingZeros(long) numberOfTrailingZeros}) are based on 19 * material from Henry S. Warren, Jr.‘s <i>Hacker‘s Delight</i>, (Addison 20 * Wesley, 2002). 21 * 22 * @author Lee Boynton 23 * @author Arthur van Hoff 24 * @author Josh Bloch 25 * @version %I%, %G% 26 * @since JDK1.0 27 */ 28 public final class Long extends Number implements Comparable<Long> { 29 /** 30 * A constant holding the minimum value a <code>long</code> can have, 31 * -2<sup>63</sup>. 32 */ 33 public static final long MIN_VALUE = http://www.mamicode.com/0x8000000000000000L; 34 35 /** 36 * A constant holding the maximum value a <code>long</code> can have, 37 * 2<sup>63</sup>-1. 38 */ 39 public static final long MAX_VALUE = http://www.mamicode.com/0x7fffffffffffffffL; 40 41 /** 42 * The <code>Class</code> instance representing the primitive type 43 * <code>long</code>. 44 * 45 * @since JDK1.1 46 */ 47 public static final Class<Long> TYPE = (Class<Long>) Class 48 .getPrimitiveClass("long"); 49 50 /** 51 * Returns a string representation of the first argument in the radix 52 * specified by the second argument. 53 * <p> 54 * If the radix is smaller than <code>Character.MIN_RADIX</code> or larger 55 * than <code>Character.MAX_RADIX</code>, then the radix <code>10</code> is 56 * used instead. 57 * <p> 58 * If the first argument is negative, the first element of the result is the 59 * ASCII minus sign <code>‘-‘</code> (<code>‘\u002d‘</code>). If the 60 * first argument is not negative, no sign character appears in the result. 61 * <p> 62 * The remaining characters of the result represent the magnitude of the 63 * first argument. If the magnitude is zero, it is represented by a single 64 * zero character <code>‘0‘</code> (<code>‘\u0030‘</code>); otherwise, 65 * the first character of the representation of the magnitude will not be 66 * the zero character. The following ASCII characters are used as digits: 67 * <blockquote> 68 * 69 * <pre> 70 * 0123456789abcdefghijklmnopqrstuvwxyz 71 * </pre> 72 * 73 * </blockquote> These are <code>‘\u0030‘</code> through 74 * <code>‘\u0039‘</code> and <code>‘\u0061‘</code> through 75 * <code>‘\u007a‘</code>. If <code>radix</code> is <var>N</var>, then 76 * the first <var>N</var> of these characters are used as radix-<var>N</var> 77 * digits in the order shown. Thus, the digits for hexadecimal (radix 16) 78 * are <code>0123456789abcdef</code>. If uppercase letters are desired, the 79 * {@link java.lang.String#toUpperCase()} method may be called on the 80 * result: <blockquote> 81 * 82 * <pre> 83 * Long.toString(n, 16).toUpperCase() 84 * </pre> 85 * 86 * </blockquote> 87 * 88 * @param i 89 * a <code>long</code>to be converted to a string. 90 * @param radix 91 * the radix to use in the string representation. 92 * @return a string representation of the argument in the specified radix. 93 * @see java.lang.Character#MAX_RADIX 94 * @see java.lang.Character#MIN_RADIX 95 */ 96 public static String toString(long i, int radix) { 97 if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) 98 radix = 10; 99 if (radix == 10) 100 return toString(i); 101 char[] buf = new char[65]; 102 int charPos = 64; 103 boolean negative = (i < 0); 104 105 if (!negative) { 106 i = -i; 107 } 108 109 while (i <= -radix) { 110 buf[charPos--] = Integer.digits[(int) (-(i % radix))]; 111 i = i / radix; 112 } 113 buf[charPos] = Integer.digits[(int) (-i)]; 114 115 if (negative) { 116 buf[--charPos] = ‘-‘; 117 } 118 119 return new String(buf, charPos, (65 - charPos)); 120 } 121 122 /** 123 * Returns a string representation of the <code>long</code> argument as an 124 * unsigned integer in base 16. 125 * <p> 126 * The unsigned <code>long</code> value is the argument plus 2<sup>64</sup> 127 * if the argument is negative; otherwise, it is equal to the argument. This 128 * value is converted to a string of ASCII digits in hexadecimal 129 * (base 16) with no extra leading <code>0</code>s. If the unsigned 130 * magnitude is zero, it is represented by a single zero character 131 * <code>‘0‘</code> (<code>‘\u0030‘</code>); otherwise, the first 132 * character of the representation of the unsigned magnitude will not be the 133 * zero character. The following characters are used as hexadecimal digits: 134 * <blockquote> 135 * 136 * <pre> 137 * 0123456789abcdef 138 * </pre> 139 * 140 * </blockquote> These are the characters <code>‘\u0030‘</code> through 141 * <code>‘\u0039‘</code> and <code>‘\u0061‘</code> through 142 * <code>‘\u0066‘</code>. If uppercase letters are desired, the 143 * {@link java.lang.String#toUpperCase()} method may be called on the 144 * result: <blockquote> 145 * 146 * <pre> 147 * Long.toHexString(n).toUpperCase() 148 * </pre> 149 * 150 * </blockquote> 151 * 152 * @param i 153 * a <code>long</code> to be converted to a string. 154 * @return the string representation of the unsigned <code>long</code> value 155 * represented by the argument in hexadecimal (base 16). 156 * @since JDK 1.0.2 157 */ 158 public static String toHexString(long i) { 159 return toUnsignedString(i, 4); 160 } 161 162 /** 163 * Returns a string representation of the <code>long</code> argument as an 164 * unsigned integer in base 8. 165 * <p> 166 * The unsigned <code>long</code> value is the argument plus 2<sup>64</sup> 167 * if the argument is negative; otherwise, it is equal to the argument. This 168 * value is converted to a string of ASCII digits in octal (base 8) 169 * with no extra leading <code>0</code>s. 170 * <p> 171 * If the unsigned magnitude is zero, it is represented by a single zero 172 * character <code>‘0‘</code> (<code>‘\u0030‘</code>); otherwise, the 173 * first character of the representation of the unsigned magnitude will not 174 * be the zero character. The following characters are used as octal digits: 175 * <blockquote> 176 * 177 * <pre> 178 * 01234567 179 * </pre> 180 * 181 * </blockquote> These are the characters <code>‘\u0030‘</code> through 182 * <code>‘\u0037‘</code>. 183 * 184 * @param i 185 * a <code>long</code> to be converted to a string. 186 * @return the string representation of the unsigned <code>long</code> value 187 * represented by the argument in octal (base 8). 188 * @since JDK 1.0.2 189 */ 190 public static String toOctalString(long i) { 191 return toUnsignedString(i, 3); 192 } 193 194 /** 195 * Returns a string representation of the <code>long</code> argument as an 196 * unsigned integer in base 2. 197 * <p> 198 * The unsigned <code>long</code> value is the argument plus 2<sup>64</sup> 199 * if the argument is negative; otherwise, it is equal to the argument. This 200 * value is converted to a string of ASCII digits in binary (base 2) 201 * with no extra leading <code>0</code>s. If the unsigned magnitude is zero, 202 * it is represented by a single zero character <code>‘0‘</code> ( 203 * <code>‘\u0030‘</code>); otherwise, the first character of the 204 * representation of the unsigned magnitude will not be the zero character. 205 * The characters <code>‘0‘</code> (<code>‘\u0030‘</code>) and 206 * <code>‘1‘</code> (<code>‘\u0031‘</code>) are used as binary digits. 207 * 208 * @param i 209 * a <code>long</code> to be converted to a string. 210 * @return the string representation of the unsigned <code>long</code> value 211 * represented by the argument in binary (base 2). 212 * @since JDK 1.0.2 213 */ 214 public static String toBinaryString(long i) { 215 return toUnsignedString(i, 1); 216 } 217 218 /** 219 * Convert the integer to an unsigned number. 220 */ 221 private static String toUnsignedString(long i, int shift) { 222 char[] buf = new char[64]; 223 int charPos = 64; 224 int radix = 1 << shift; 225 long mask = radix - 1; 226 do { 227 buf[--charPos] = Integer.digits[(int) (i & mask)]; 228 i >>>= shift; 229 } while (i != 0); 230 return new String(buf, charPos, (64 - charPos)); 231 } 232 233 /** 234 * Returns a <code>String</code> object representing the specified 235 * <code>long</code>. The argument is converted to signed decimal 236 * representation and returned as a string, exactly as if the argument and 237 * the radix 10 were given as arguments to the {@link #toString(long, int)} 238 * method. 239 * 240 * @param i 241 * a <code>long</code> to be converted. 242 * @return a string representation of the argument in base 10. 243 */ 244 public static String toString(long i) { 245 if (i == Long.MIN_VALUE) 246 return "-9223372036854775808"; 247 int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i); 248 char[] buf = new char[size]; 249 getChars(i, size, buf); 250 return new String(0, size, buf); 251 } 252 253 /** 254 * Places characters representing the integer i into the character array 255 * buf. The characters are placed into the buffer backwards starting with 256 * the least significant digit at the specified index (exclusive), and 257 * working backwards from there. 258 * 259 * Will fail if i == Long.MIN_VALUE 260 */ 261 static void getChars(long i, int index, char[] buf) { 262 long q; 263 int r; 264 int charPos = index; 265 char sign = 0; 266 267 if (i < 0) { 268 sign = ‘-‘; 269 i = -i; 270 } 271 272 // Get 2 digits/iteration using longs until quotient fits into an int 273 while (i > Integer.MAX_VALUE) { 274 q = i / 100; 275 // really: r = i - (q * 100); 276 r = (int) (i - ((q << 6) + (q << 5) + (q << 2))); 277 i = q; 278 buf[--charPos] = Integer.DigitOnes[r]; 279 buf[--charPos] = Integer.DigitTens[r]; 280 } 281 282 // Get 2 digits/iteration using ints 283 int q2; 284 int i2 = (int) i; 285 while (i2 >= 65536) { 286 q2 = i2 / 100; 287 // really: r = i2 - (q * 100); 288 r = i2 - ((q2 << 6) + (q2 << 5) + (q2 << 2)); 289 i2 = q2; 290 buf[--charPos] = Integer.DigitOnes[r]; 291 buf[--charPos] = Integer.DigitTens[r]; 292 } 293 294 // Fall thru to fast mode for smaller numbers 295 // assert(i2 <= 65536, i2); 296 for (;;) { 297 q2 = (i2 * 52429) >>> (16 + 3); 298 r = i2 - ((q2 << 3) + (q2 << 1)); // r = i2-(q2*10) ... 299 buf[--charPos] = Integer.digits[r]; 300 i2 = q2; 301 if (i2 == 0) 302 break; 303 } 304 if (sign != 0) { 305 buf[--charPos] = sign; 306 } 307 } 308 309 // Requires positive x 310 static int stringSize(long x) { 311 long p = 10; 312 for (int i = 1; i < 19; i++) { 313 if (x < p) 314 return i; 315 p = 10 * p; 316 } 317 return 19; 318 } 319 320 private static final long MULTMIN_RADIX_TEN = Long.MIN_VALUE / 10; 321 private static final long N_MULTMAX_RADIX_TEN = -Long.MAX_VALUE / 10; 322 323 /** 324 * Parses the string argument as a signed <code>long</code> in the radix 325 * specified by the second argument. The characters in the string must all 326 * be digits of the specified radix (as determined by whether 327 * {@link java.lang.Character#digit(char, int)} returns a nonnegative 328 * value), except that the first character may be an ASCII minus sign 329 * <code>‘-‘</code> (<code>‘\u002D‘</code>) to indicate a negative 330 * value. The resulting <code>long</code> value is returned. 331 * <p> 332 * Note that neither the character <code>L</code> (<code>‘\u004C‘</code> 333 * ) nor <code>l</code> (<code>‘\u006C‘</code>) is permitted to appear 334 * at the end of the string as a type indicator, as would be permitted in 335 * Java programming language source code - except that either <code>L</code> 336 * or <code>l</code> may appear as a digit for a radix greater than 22. 337 * <p> 338 * An exception of type <code>NumberFormatException</code> is thrown if any 339 * of the following situations occurs: 340 * <ul> 341 * <li>The first argument is <code>null</code> or is a string of length 342 * zero. 343 * <li>The <code>radix</code> is either smaller than 344 * {@link java.lang.Character#MIN_RADIX} or larger than 345 * {@link java.lang.Character#MAX_RADIX}. 346 * <li>Any character of the string is not a digit of the specified radix, 347 * except that the first character may be a minus sign <code>‘-‘</code> ( 348 * <code>‘\u002d‘</code>) provided that the string is longer than length 349 * 1. 350 * <li>The value represented by the string is not a value of type 351 * <code>long</code>. 352 * </ul> 353 * <p> 354 * Examples: <blockquote> 355 * 356 * <pre> 357 * parseLong("0", 10) returns 0L 358 * parseLong("473", 10) returns 473L 359 * parseLong("-0", 10) returns 0L 360 * parseLong("-FF", 16) returns -255L 361 * parseLong("1100110", 2) returns 102L 362 * parseLong("99", 8) throws a NumberFormatException 363 * parseLong("Hazelnut", 10) throws a NumberFormatException 364 * parseLong("Hazelnut", 36) returns 1356099454469L 365 * </pre> 366 * 367 * </blockquote> 368 * 369 * @param s 370 * the <code>String</code> containing the <code>long</code> 371 * representation to be parsed. 372 * @param radix 373 * the radix to be used while parsing <code>s</code>. 374 * @return the <code>long</code> represented by the string argument in the 375 * specified radix. 376 * @exception NumberFormatException 377 * if the string does not contain a parsable 378 * <code>long</code>. 379 */ 380 public static long parseLong(String s, int radix) 381 throws NumberFormatException { 382 if (s == null) { 383 throw new NumberFormatException("null"); 384 } 385 386 if (radix < Character.MIN_RADIX) { 387 throw new NumberFormatException("radix " + radix 388 + " less than Character.MIN_RADIX"); 389 } 390 if (radix > Character.MAX_RADIX) { 391 throw new NumberFormatException("radix " + radix 392 + " greater than Character.MAX_RADIX"); 393 } 394 395 long result = 0; 396 boolean negative = false; 397 int i = 0, max = s.length(); 398 long limit; 399 long multmin; 400 int digit; 401 402 if (max > 0) { 403 if (s.charAt(0) == ‘-‘) { 404 negative = true; 405 limit = Long.MIN_VALUE; 406 i++; 407 } else { 408 limit = -Long.MAX_VALUE; 409 } 410 if (radix == 10) { 411 multmin = negative ? MULTMIN_RADIX_TEN : N_MULTMAX_RADIX_TEN; 412 } else { 413 multmin = limit / radix; 414 } 415 if (i < max) { 416 digit = Character.digit(s.charAt(i++), radix); 417 if (digit < 0) { 418 throw NumberFormatException.forInputString(s); 419 } else { 420 result = -digit; 421 } 422 } 423 while (i < max) { 424 // Accumulating negatively avoids surprises near MAX_VALUE 425 digit = Character.digit(s.charAt(i++), radix); 426 if (digit < 0) { 427 throw NumberFormatException.forInputString(s); 428 } 429 if (result < multmin) { 430 throw NumberFormatException.forInputString(s); 431 } 432 result *= radix; 433 if (result < limit + digit) { 434 throw NumberFormatException.forInputString(s); 435 } 436 result -= digit; 437 } 438 } else { 439 throw NumberFormatException.forInputString(s); 440 } 441 if (negative) { 442 if (i > 1) { 443 return result; 444 } else { /* Only got "-" */ 445 throw NumberFormatException.forInputString(s); 446 } 447 } else { 448 return -result; 449 } 450 } 451 452 /** 453 * Parses the string argument as a signed decimal <code>long</code>. The 454 * characters in the string must all be decimal digits, except that the 455 * first character may be an ASCII minus sign <code>‘-‘</code> ( 456 * <code>\u002D‘</code>) to indicate a negative value. The resulting 457 * <code>long</code> value is returned, exactly as if the argument and the 458 * radix <code>10</code> were given as arguments to the 459 * {@link #parseLong(java.lang.String, int)} method. 460 * <p> 461 * Note that neither the character <code>L</code> (<code>‘\u004C‘</code> 462 * ) nor <code>l</code> (<code>‘\u006C‘</code>) is permitted to appear 463 * at the end of the string as a type indicator, as would be permitted in 464 * Java programming language source code. 465 * 466 * @param s 467 * a <code>String</code> containing the <code>long</code> 468 * representation to be parsed 469 * @return the <code>long</code> represented by the argument in decimal. 470 * @exception NumberFormatException 471 * if the string does not contain a parsable 472 * <code>long</code>. 473 */ 474 public static long parseLong(String s) throws NumberFormatException { 475 return parseLong(s, 10); 476 } 477 478 /** 479 * Returns a <code>Long</code> object holding the value extracted from the 480 * specified <code>String</code> when parsed with the radix given by the 481 * second argument. The first argument is interpreted as representing a 482 * signed <code>long</code> in the radix specified by the second argument, 483 * exactly as if the arguments were given to the 484 * {@link #parseLong(java.lang.String, int)} method. The result is a 485 * <code>Long</code> object that represents the <code>long</code> value 486 * specified by the string. 487 * <p> 488 * In other words, this method returns a <code>Long</code> object equal to 489 * the value of: 490 * 491 * <blockquote><code> 492 * new Long(Long.parseLong(s, radix)) 493 * </code></blockquote> 494 * 495 * @param s 496 * the string to be parsed 497 * @param radix 498 * the radix to be used in interpreting <code>s</code> 499 * @return a <code>Long</code> object holding the value represented by the 500 * string argument in the specified radix. 501 * @exception NumberFormatException 502 * If the <code>String</code> does not contain a parsable 503 * <code>long</code>. 504 */ 505 public static Long valueOf(String s, int radix) 506 throws NumberFormatException { 507 return Long.valueOf(parseLong(s, radix)); 508 } 509 510 /** 511 * Returns a <code>Long</code> object holding the value of the specified 512 * <code>String</code>. The argument is interpreted as representing a signed 513 * decimal <code>long</code>, exactly as if the argument were given to the 514 * {@link #parseLong(java.lang.String)} method. The result is a 515 * <code>Long</code> object that represents the integer value specified by 516 * the string. 517 * <p> 518 * In other words, this method returns a <code>Long</code> object equal to 519 * the value of: 520 * 521 * <blockquote> 522 * 523 * <pre> 524 * new Long(Long.parseLong(s)) 525 * </pre> 526 * 527 * </blockquote> 528 * 529 * @param s 530 * the string to be parsed. 531 * @return a <code>Long</code> object holding the value represented by the 532 * string argument. 533 * @exception NumberFormatException 534 * If the string cannot be parsed as a <code>long</code>. 535 */ 536 public static Long valueOf(String s) throws NumberFormatException { 537 return Long.valueOf(parseLong(s, 10)); 538 } 539 540 private static class LongCache { 541 private LongCache() { 542 } 543 544 static final Long cache[] = new Long[-(-128) + 127 + 1]; 545 546 static { 547 for (int i = 0; i < cache.length; i++) 548 cache[i] = new Long(i - 128); 549 } 550 } 551 552 /** 553 * Returns a <tt>Long</tt> instance representing the specified <tt>long</tt> 554 * value. If a new <tt>Long</tt> instance is not required, this method 555 * should generally be used in preference to the constructor 556 * {@link #Long(long)}, as this method is likely to yield significantly 557 * better space and time performance by caching frequently requested values. 558 * 559 * @param l 560 * a long value. 561 * @return a <tt>Long</tt> instance representing <tt>l</tt>. 562 * @since 1.5 563 */ 564 public static Long valueOf(long l) { 565 final int offset = 128; 566 if (l >= -128 && l <= 127) { // will cache 567 return LongCache.cache[(int) l + offset]; 568 } 569 return new Long(l); 570 } 571 572 /** 573 * Decodes a <code>String</code> into a <code>Long</code>. Accepts decimal, 574 * hexadecimal, and octal numbers given by the following grammar: 575 * 576 * <blockquote> 577 * <dl> 578 * <dt><i>DecodableString:</i> 579 * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i> 580 * <dd><i>Sign<sub>opt</sub></i> <code>0x</code> <i>HexDigits</i> 581 * <dd><i>Sign<sub>opt</sub></i> <code>0X</code> <i>HexDigits</i> 582 * <dd><i>Sign<sub>opt</sub></i> <code>#</code> <i>HexDigits</i> 583 * <dd><i>Sign<sub>opt</sub></i> <code>0</code> <i>OctalDigits</i> 584 * <p> 585 * <dt><i>Sign:</i> 586 * <dd><code>-</code> 587 * </dl> 588 * </blockquote> 589 * 590 * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i> are 591 * defined in <a href= http://www.mamicode.com/ 592 * "http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#48282" 593 * >§3.10.1</a> of the <a 594 * href="http://www.mamicode.com/http://java.sun.com/docs/books/jls/html/">Java Language 595 * Specification</a>. 596 * <p> 597 * The sequence of characters following an (optional) negative sign and/or 598 * radix specifier ("<code>0x</code>", "<code>0X</code> 599 * ", "<code>#</code>", or leading zero) is parsed as by the 600 * <code>Long.parseLong</code> method with the indicated radix (10, 16, or 601 * 8). This sequence of characters must represent a positive value or a 602 * {@link NumberFormatException} will be thrown. The result is negated if 603 * first character of the specified <code>String</code> is the minus sign. 604 * No whitespace characters are permitted in the <code>String</code>. 605 * 606 * @param nm 607 * the <code>String</code> to decode. 608 * @return a <code>Long</code> object holding the <code>long</code> value 609 * represented by <code>nm</code> 610 * @exception NumberFormatException 611 * if the <code>String</code> does not contain a parsable 612 * <code>long</code>. 613 * @see java.lang.Long#parseLong(String, int) 614 * @since 1.2 615 */ 616 public static Long decode(String nm) throws NumberFormatException { 617 int radix = 10; 618 int index = 0; 619 boolean negative = false; 620 Long result; 621 622 // Handle minus sign, if present 623 if (nm.startsWith("-")) { 624 negative = true; 625 index++; 626 } 627 628 // Handle radix specifier, if present 629 if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) { 630 index += 2; 631 radix = 16; 632 } else if (nm.startsWith("#", index)) { 633 index++; 634 radix = 16; 635 } else if (nm.startsWith("0", index) && nm.length() > 1 + index) { 636 index++; 637 radix = 8; 638 } 639 640 if (nm.startsWith("-", index)) 641 throw new NumberFormatException("Negative sign in wrong position"); 642 643 try { 644 result = Long.valueOf(nm.substring(index), radix); 645 result = negative ? Long.valueOf(-result.longValue()) : result; 646 } catch (NumberFormatException e) { 647 // If number is Long.MIN_VALUE, we‘ll end up here. The next line 648 // handles this case, and causes any genuine format error to be 649 // rethrown. 650 String constant = negative ? "-" + nm.substring(index) : nm 651 .substring(index); 652 result = Long.valueOf(constant, radix); 653 } 654 return result; 655 } 656 657 /** 658 * The value of the <code>Long</code>. 659 * 660 * @serial 661 */ 662 private final long value; 663 664 /** 665 * Constructs a newly allocated <code>Long</code> object that represents the 666 * specified <code>long</code> argument. 667 * 668 * @param value 669 * the value to be represented by the <code>Long</code> object. 670 */ 671 public Long(long value) { 672 this.value =http://www.mamicode.com/ value; 673 } 674 675 /** 676 * Constructs a newly allocated <code>Long</code> object that represents the 677 * <code>long</code> value indicated by the <code>String</code> parameter. 678 * The string is converted to a <code>long</code> value in exactly the 679 * manner used by the <code>parseLong</code> method for radix 10. 680 * 681 * @param s 682 * the <code>String</code> to be converted to a <code>Long</code> 683 * . 684 * @exception NumberFormatException 685 * if the <code>String</code> does not contain a parsable 686 * <code>long</code>. 687 * @see java.lang.Long#parseLong(java.lang.String, int) 688 */ 689 public Long(String s) throws NumberFormatException { 690 this.value = http://www.mamicode.com/parseLong(s, 10); 691 } 692 693 /** 694 * Returns the value of this <code>Long</code> as a <code>byte</code>. 695 */ 696 public byte byteValue() { 697 return (byte) value; 698 } 699 700 /** 701 * Returns the value of this <code>Long</code> as a <code>short</code>. 702 */ 703 public short shortValue() { 704 return (short) value; 705 } 706 707 /** 708 * Returns the value of this <code>Long</code> as an <code>int</code>. 709 */ 710 public int intValue() { 711 return (int) value; 712 } 713 714 /** 715 * Returns the value of this <code>Long</code> as a <code>long</code> value. 716 */ 717 public long longValue() { 718 return (long) value; 719 } 720 721 /** 722 * Returns the value of this <code>Long</code> as a <code>float</code>. 723 */ 724 public float floatValue() { 725 return (float) value; 726 } 727 728 /** 729 * Returns the value of this <code>Long</code> as a <code>double</code>. 730 */ 731 public double doubleValue() { 732 return (double) value; 733 } 734 735 /** 736 * Returns a <code>String</code> object representing this <code>Long</code> 737 * ‘s value. The value is converted to signed decimal representation and 738 * returned as a string, exactly as if the <code>long</code> value were 739 * given as an argument to the {@link java.lang.Long#toString(long)} method. 740 * 741 * @return a string representation of the value of this object in 742 * base 10. 743 */ 744 public String toString() { 745 return String.valueOf(value); 746 } 747 748 /** 749 * Returns a hash code for this <code>Long</code>. The result is the 750 * exclusive OR of the two halves of the primitive <code>long</code> value 751 * held by this <code>Long</code> object. That is, the hashcode is the value 752 * of the expression: <blockquote> 753 * 754 * <pre> 755 * (int) (this.longValue() ˆ (this.longValue() >>> 32)) 756 * </pre> 757 * 758 * </blockquote> 759 * 760 * @return a hash code value for this object. 761 */ 762 public int hashCode() { 763 return (int) (value ^ (value >>> 32)); 764 } 765 766 /** 767 * Compares this object to the specified object. The result is 768 * <code>true</code> if and only if the argument is not <code>null</code> 769 * and is a <code>Long</code> object that contains the same 770 * <code>long</code> value as this object. 771 * 772 * @param obj 773 * the object to compare with. 774 * @return <code>true</code> if the objects are the same; <code>false</code> 775 * otherwise. 776 */ 777 public boolean equals(Object obj) { 778 if (obj instanceof Long) { 779 return value =http://www.mamicode.com/= ((Long) obj).longValue(); 780 } 781 return false; 782 } 783 784 /** 785 * Determines the <code>long</code> value of the system property with the 786 * specified name. 787 * <p> 788 * The first argument is treated as the name of a system property. System 789 * properties are accessible through the 790 * {@link java.lang.System#getProperty(java.lang.String)} method. The string 791 * value of this property is then interpreted as a <code>long</code> value 792 * and a <code>Long</code> object representing this value is returned. 793 * Details of possible numeric formats can be found with the definition of 794 * <code>getProperty</code>. 795 * <p> 796 * If there is no property with the specified name, if the specified name is 797 * empty or <code>null</code>, or if the property does not have the correct 798 * numeric format, then <code>null</code> is returned. 799 * <p> 800 * In other words, this method returns a <code>Long</code> object equal to 801 * the value of: <blockquote><code> 802 * getLong(nm, null) 803 * </code></blockquote> 804 * 805 * @param nm 806 * property name. 807 * @return the <code>Long</code> value of the property. 808 * @see java.lang.System#getProperty(java.lang.String) 809 * @see java.lang.System#getProperty(java.lang.String, java.lang.String) 810 */ 811 public static Long getLong(String nm) { 812 return getLong(nm, null); 813 } 814 815 /** 816 * Determines the <code>long</code> value of the system property with the 817 * specified name. 818 * <p> 819 * The first argument is treated as the name of a system property. System 820 * properties are accessible through the 821 * {@link java.lang.System#getProperty(java.lang.String)} method. The string 822 * value of this property is then interpreted as a <code>long</code> value 823 * and a <code>Long</code> object representing this value is returned. 824 * Details of possible numeric formats can be found with the definition of 825 * <code>getProperty</code>. 826 * <p> 827 * The second argument is the default value. A <code>Long</code> object that 828 * represents the value of the second argument is returned if there is no 829 * property of the specified name, if the property does not have the correct 830 * numeric format, or if the specified name is empty or null. 831 * <p> 832 * In other words, this method returns a <code>Long</code> object equal to 833 * the value of: <blockquote><code> 834 * getLong(nm, new Long(val)) 835 * </code></blockquote> but in practice it may be implemented in a manner 836 * such as: <blockquote> 837 * 838 * <pre> 839 * Long result = getLong(nm, null); 840 * return (result == null) ? new Long(val) : result; 841 * </pre> 842 * 843 * </blockquote> to avoid the unnecessary allocation of a <code>Long</code> 844 * object when the default value is not needed. 845 * 846 * @param nm 847 * property name. 848 * @param val 849 * default value. 850 * @return the <code>Long</code> value of the property. 851 * @see java.lang.System#getProperty(java.lang.String) 852 * @see java.lang.System#getProperty(java.lang.String, java.lang.String) 853 */ 854 public static Long getLong(String nm, long val) { 855 Long result = Long.getLong(nm, null); 856 return (result == null) ? Long.valueOf(val) : result; 857 } 858 859 /** 860 * Returns the <code>long</code> value of the system property with the 861 * specified name. The first argument is treated as the name of a system 862 * property. System properties are accessible through the 863 * {@link java.lang.System#getProperty(java.lang.String)} method. The string 864 * value of this property is then interpreted as a <code>long</code> value, 865 * as per the <code>Long.decode</code> method, and a <code>Long</code> 866 * object representing this value is returned. 867 * <p> 868 * <ul> 869 * <li>If the property value begins with the two ASCII characters 870 * <code>0x</code> or the ASCII character <code>#</code>, not followed by a 871 * minus sign, then the rest of it is parsed as a hexadecimal integer 872 * exactly as for the method {@link #valueOf(java.lang.String, int)} with 873 * radix 16. 874 * <li>If the property value begins with the ASCII character <code>0</code> 875 * followed by another character, it is parsed as an octal integer exactly 876 * as by the method {@link #valueOf(java.lang.String, int)} with radix 8. 877 * <li>Otherwise the property value is parsed as a decimal integer exactly 878 * as by the method {@link #valueOf(java.lang.String, int)} with radix 10. 879 * </ul> 880 * <p> 881 * Note that, in every case, neither <code>L</code> ( 882 * <code>‘\u004C‘</code>) nor <code>l</code> (<code>‘\u006C‘</code>) 883 * is permitted to appear at the end of the property value as a type 884 * indicator, as would be permitted in Java programming language source 885 * code. 886 * <p> 887 * The second argument is the default value. The default value is returned 888 * if there is no property of the specified name, if the property does not 889 * have the correct numeric format, or if the specified name is empty or 890 * <code>null</code>. 891 * 892 * @param nm 893 * property name. 894 * @param val 895 * default value. 896 * @return the <code>Long</code> value of the property. 897 * @see java.lang.System#getProperty(java.lang.String) 898 * @see java.lang.System#getProperty(java.lang.String, java.lang.String) 899 * @see java.lang.Long#decode 900 */ 901 public static Long getLong(String nm, Long val) { 902 String v = null; 903 try { 904 v = System.getProperty(nm); 905 } catch (IllegalArgumentException e) { 906 } catch (NullPointerException e) { 907 } 908 if (v != null) { 909 try { 910 return Long.decode(v); 911 } catch (NumberFormatException e) { 912 } 913 } 914 return val; 915 } 916 917 /** 918 * Compares two <code>Long</code> objects numerically. 919 * 920 * @param anotherLong 921 * the <code>Long</code> to be compared. 922 * @return the value <code>0</code> if this <code>Long</code> is equal to 923 * the argument <code>Long</code>; a value less than <code>0</code> 924 * if this <code>Long</code> is numerically less than the argument 925 * <code>Long</code>; and a value greater than <code>0</code> if 926 * this <code>Long</code> is numerically greater than the argument 927 * <code>Long</code> (signed comparison). 928 * @since 1.2 929 */ 930 public int compareTo(Long anotherLong) { 931 long thisVal = this.value; 932 long anotherVal = anotherLong.value; 933 return (thisVal < anotherVal ? -1 : (thisVal == anotherVal ? 0 : 1)); 934 } 935 936 // Bit Twiddling 937 938 /** 939 * The number of bits used to represent a <tt>long</tt> value in two‘s 940 * complement binary form. 941 * 942 * @since 1.5 943 */ 944 public static final int SIZE = 64; 945 946 /** 947 * Returns a <tt>long</tt> value with at most a single one-bit, in the 948 * position of the highest-order ("leftmost") one-bit in the specified 949 * <tt>long</tt> value. Returns zero if the specified value has no one-bits 950 * in its two‘s complement binary representation, that is, if it is equal to 951 * zero. 952 * 953 * @return a <tt>long</tt> value with a single one-bit, in the position of 954 * the highest-order one-bit in the specified value, or zero if the 955 * specified value is itself equal to zero. 956 * @since 1.5 957 */ 958 public static long highestOneBit(long i) { 959 // HD, Figure 3-1 960 i |= (i >> 1); 961 i |= (i >> 2); 962 i |= (i >> 4); 963 i |= (i >> 8); 964 i |= (i >> 16); 965 i |= (i >> 32); 966 return i - (i >>> 1); 967 } 968 969 /** 970 * Returns a <tt>long</tt> value with at most a single one-bit, in the 971 * position of the lowest-order ("rightmost") one-bit in the specified 972 * <tt>long</tt> value. Returns zero if the specified value has no one-bits 973 * in its two‘s complement binary representation, that is, if it is equal to 974 * zero. 975 * 976 * @return a <tt>long</tt> value with a single one-bit, in the position of 977 * the lowest-order one-bit in the specified value, or zero if the 978 * specified value is itself equal to zero. 979 * @since 1.5 980 */ 981 public static long lowestOneBit(long i) { 982 // HD, Section 2-1 983 return i & -i; 984 } 985 986 /** 987 * Returns the number of zero bits preceding the highest-order ("leftmost") 988 * one-bit in the two‘s complement binary representation of the specified 989 * <tt>long</tt> value. Returns 64 if the specified value has no one-bits in 990 * its two‘s complement representation, in other words if it is equal to 991 * zero. 992 * 993 * <p> 994 * Note that this method is closely related to the logarithm base 2. For all 995 * positive <tt>long</tt> values x: 996 * <ul> 997 * <li>floor(log<sub>2</sub>(x)) = <tt>63 - numberOfLeadingZeros(x)</tt> 998 * <li>ceil(log<sub>2</sub>(x)) = <tt>64 - numberOfLeadingZeros(x - 1)</tt> 999 * </ul> 1000 * 1001 * @return the number of zero bits preceding the highest-order ("leftmost") 1002 * one-bit in the two‘s complement binary representation of the 1003 * specified <tt>long</tt> value, or 64 if the value is equal to 1004 * zero. 1005 * @since 1.5 1006 */ 1007 public static int numberOfLeadingZeros(long i) { 1008 // HD, Figure 5-6 1009 if (i == 0) 1010 return 64; 1011 int n = 1; 1012 int x = (int) (i >>> 32); 1013 if (x == 0) { 1014 n += 32; 1015 x = (int) i; 1016 } 1017 if (x >>> 16 == 0) { 1018 n += 16; 1019 x <<= 16; 1020 } 1021 if (x >>> 24 == 0) { 1022 n += 8; 1023 x <<= 8; 1024 } 1025 if (x >>> 28 == 0) { 1026 n += 4; 1027 x <<= 4; 1028 } 1029 if (x >>> 30 == 0) { 1030 n += 2; 1031 x <<= 2; 1032 } 1033 n -= x >>> 31; 1034 return n; 1035 } 1036 1037 /** 1038 * Returns the number of zero bits following the lowest-order ("rightmost") 1039 * one-bit in the two‘s complement binary representation of the specified 1040 * <tt>long</tt> value. Returns 64 if the specified value has no one-bits in 1041 * its two‘s complement representation, in other words if it is equal to 1042 * zero. 1043 * 1044 * @return the number of zero bits following the lowest-order ("rightmost") 1045 * one-bit in the two‘s complement binary representation of the 1046 * specified <tt>long</tt> value, or 64 if the value is equal to 1047 * zero. 1048 * @since 1.5 1049 */ 1050 public static int numberOfTrailingZeros(long i) { 1051 // HD, Figure 5-14 1052 int x, y; 1053 if (i == 0) 1054 return 64; 1055 int n = 63; 1056 y = (int) i; 1057 if (y != 0) { 1058 n = n - 32; 1059 x = y; 1060 } else 1061 x = (int) (i >>> 32); 1062 y = x << 16; 1063 if (y != 0) { 1064 n = n - 16; 1065 x = y; 1066 } 1067 y = x << 8; 1068 if (y != 0) { 1069 n = n - 8; 1070 x = y; 1071 } 1072 y = x << 4; 1073 if (y != 0) { 1074 n = n - 4; 1075 x = y; 1076 } 1077 y = x << 2; 1078 if (y != 0) { 1079 n = n - 2; 1080 x = y; 1081 } 1082 return n - ((x << 1) >>> 31); 1083 } 1084 1085 /** 1086 * Returns the number of one-bits in the two‘s complement binary 1087 * representation of the specified <tt>long</tt> value. This function is 1088 * sometimes referred to as the <i>population count</i>. 1089 * 1090 * @return the number of one-bits in the two‘s complement binary 1091 * representation of the specified <tt>long</tt> value. 1092 * @since 1.5 1093 */ 1094 public static int bitCount(long i) { 1095 // HD, Figure 5-14 1096 i = i - ((i >>> 1) & 0x5555555555555555L); 1097 i = (i & 0x3333333333333333L) + ((i >>> 2) & 0x3333333333333333L); 1098 i = (i + (i >>> 4)) & 0x0f0f0f0f0f0f0f0fL; 1099 i = i + (i >>> 8); 1100 i = i + (i >>> 16); 1101 i = i + (i >>> 32); 1102 return (int) i & 0x7f; 1103 } 1104 1105 /** 1106 * Returns the value obtained by rotating the two‘s complement binary 1107 * representation of the specified <tt>long</tt> value left by the specified 1108 * number of bits. (Bits shifted out of the left hand, or high-order, side 1109 * reenter on the right, or low-order.) 1110 * 1111 * <p> 1112 * Note that left rotation with a negative distance is equivalent to right 1113 * rotation: <tt>rotateLeft(val, -distance) == rotateRight(val, 1114 * distance)</tt>. Note also that rotation by any multiple of 64 is a no-op, 1115 * so all but the last six bits of the rotation distance can be ignored, 1116 * even if the distance is negative: <tt>rotateLeft(val, 1117 * distance) == rotateLeft(val, distance & 0x3F)</tt>. 1118 * 1119 * @return the value obtained by rotating the two‘s complement binary 1120 * representation of the specified <tt>long</tt> value left by the 1121 * specified number of bits. 1122 * @since 1.5 1123 */ 1124 public static long rotateLeft(long i, int distance) { 1125 return (i << distance) | (i >>> -distance); 1126 } 1127 1128 /** 1129 * Returns the value obtained by rotating the two‘s complement binary 1130 * representation of the specified <tt>long</tt> value right by the 1131 * specified number of bits. (Bits shifted out of the right hand, or 1132 * low-order, side reenter on the left, or high-order.) 1133 * 1134 * <p> 1135 * Note that right rotation with a negative distance is equivalent to left 1136 * rotation: <tt>rotateRight(val, -distance) == rotateLeft(val, 1137 * distance)</tt>. Note also that rotation by any multiple of 64 is a no-op, 1138 * so all but the last six bits of the rotation distance can be ignored, 1139 * even if the distance is negative: <tt>rotateRight(val, 1140 * distance) == rotateRight(val, distance & 0x3F)</tt>. 1141 * 1142 * @return the value obtained by rotating the two‘s complement binary 1143 * representation of the specified <tt>long</tt> value right by the 1144 * specified number of bits. 1145 * @since 1.5 1146 */ 1147 public static long rotateRight(long i, int distance) { 1148 return (i >>> distance) | (i << -distance); 1149 } 1150 1151 /** 1152 * Returns the value obtained by reversing the order of the bits in the 1153 * two‘s complement binary representation of the specified <tt>long</tt> 1154 * value. 1155 * 1156 * @return the value obtained by reversing order of the bits in the 1157 * specified <tt>long</tt> value. 1158 * @since 1.5 1159 */ 1160 public static long reverse(long i) { 1161 // HD, Figure 7-1 1162 i = (i & 0x5555555555555555L) << 1 | (i >>> 1) & 0x5555555555555555L; 1163 i = (i & 0x3333333333333333L) << 2 | (i >>> 2) & 0x3333333333333333L; 1164 i = (i & 0x0f0f0f0f0f0f0f0fL) << 4 | (i >>> 4) & 0x0f0f0f0f0f0f0f0fL; 1165 i = (i & 0x00ff00ff00ff00ffL) << 8 | (i >>> 8) & 0x00ff00ff00ff00ffL; 1166 i = (i << 48) | ((i & 0xffff0000L) << 16) | ((i >>> 16) & 0xffff0000L) 1167 | (i >>> 48); 1168 return i; 1169 } 1170 1171 /** 1172 * Returns the signum function of the specified <tt>long</tt> value. (The 1173 * return value is -1 if the specified value is negative; 0 if the specified 1174 * value is zero; and 1 if the specified value is positive.) 1175 * 1176 * @return the signum function of the specified <tt>long</tt> value. 1177 * @since 1.5 1178 */ 1179 public static int signum(long i) { 1180 // HD, Section 2-7 1181 return (int) ((i >> 63) | (-i >>> 63)); 1182 } 1183 1184 /** 1185 * Returns the value obtained by reversing the order of the bytes in the 1186 * two‘s complement representation of the specified <tt>long</tt> value. 1187 * 1188 * @return the value obtained by reversing the bytes in the specified 1189 * <tt>long</tt> value. 1190 * @since 1.5 1191 */ 1192 public static long reverseBytes(long i) { 1193 i = (i & 0x00ff00ff00ff00ffL) << 8 | (i >>> 8) & 0x00ff00ff00ff00ffL; 1194 return (i << 48) | ((i & 0xffff0000L) << 16) 1195 | ((i >>> 16) & 0xffff0000L) | (i >>> 48); 1196 } 1197 1198 /** use serialVersionUID from JDK 1.0.2 for interoperability */ 1199 private static final long serialVersionUID = 4290774380558885855L; 1200 }
java.lang.Long
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。