1 /* 2 * Copyright (c) 1994, 2018, 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 java.lang.annotation.Native; 29 import java.math.*; 30 import java.util.Objects; 31 import jdk.internal.HotSpotIntrinsicCandidate; 32 33 import static java.lang.String.COMPACT_STRINGS; 34 import static java.lang.String.LATIN1; 35 import static java.lang.String.UTF16; 36 37 /** 38 * The {@code Long} class wraps a value of the primitive type {@code 39 * long} in an object. An object of type {@code Long} contains a 40 * single field whose type is {@code long}. 41 * 42 * <p> In addition, this class provides several methods for converting 43 * a {@code long} to a {@code String} and a {@code String} to a {@code 44 * long}, as well as other constants and methods useful when dealing 45 * with a {@code long}. 46 * 47 * <p>Implementation note: The implementations of the "bit twiddling" 48 * methods (such as {@link #highestOneBit(long) highestOneBit} and 49 * {@link #numberOfTrailingZeros(long) numberOfTrailingZeros}) are 50 * based on material from Henry S. Warren, Jr.'s <i>Hacker's 51 * Delight</i>, (Addison Wesley, 2002). 52 * 53 * @author Lee Boynton 54 * @author Arthur van Hoff 55 * @author Josh Bloch 56 * @author Joseph D. Darcy 57 * @since 1.0 58 */ 59 public final class Long extends Number implements Comparable<Long> { 60 /** 61 * A constant holding the minimum value a {@code long} can 62 * have, -2<sup>63</sup>. 63 */ 64 @Native public static final long MIN_VALUE = 0x8000000000000000L; 65 66 /** 67 * A constant holding the maximum value a {@code long} can 68 * have, 2<sup>63</sup>-1. 69 */ 70 @Native public static final long MAX_VALUE = 0x7fffffffffffffffL; 71 72 /** 73 * The {@code Class} instance representing the primitive type 74 * {@code long}. 75 * 76 * @since 1.1 77 */ 78 @SuppressWarnings("unchecked") 79 public static final Class<Long> TYPE = (Class<Long>) Class.getPrimitiveClass("long"); 80 81 /** 82 * Returns a string representation of the first argument in the 83 * radix specified by the second argument. 84 * 85 * <p>If the radix is smaller than {@code Character.MIN_RADIX} 86 * or larger than {@code Character.MAX_RADIX}, then the radix 87 * {@code 10} is used instead. 88 * 89 * <p>If the first argument is negative, the first element of the 90 * result is the ASCII minus sign {@code '-'} 91 * ({@code '\u005Cu002d'}). If the first argument is not 92 * negative, no sign character appears in the result. 93 * 94 * <p>The remaining characters of the result represent the magnitude 95 * of the first argument. If the magnitude is zero, it is 96 * represented by a single zero character {@code '0'} 97 * ({@code '\u005Cu0030'}); otherwise, the first character of 98 * the representation of the magnitude will not be the zero 99 * character. The following ASCII characters are used as digits: 100 * 101 * <blockquote> 102 * {@code 0123456789abcdefghijklmnopqrstuvwxyz} 103 * </blockquote> 104 * 105 * These are {@code '\u005Cu0030'} through 106 * {@code '\u005Cu0039'} and {@code '\u005Cu0061'} through 107 * {@code '\u005Cu007a'}. If {@code radix} is 108 * <var>N</var>, then the first <var>N</var> of these characters 109 * are used as radix-<var>N</var> digits in the order shown. Thus, 110 * the digits for hexadecimal (radix 16) are 111 * {@code 0123456789abcdef}. If uppercase letters are 112 * desired, the {@link java.lang.String#toUpperCase()} method may 113 * be called on the result: 114 * 115 * <blockquote> 116 * {@code Long.toString(n, 16).toUpperCase()} 117 * </blockquote> 118 * 119 * @param i a {@code long} to be converted to a string. 120 * @param radix the radix to use in the string representation. 121 * @return a string representation of the argument in the specified radix. 122 * @see java.lang.Character#MAX_RADIX 123 * @see java.lang.Character#MIN_RADIX 124 */ 125 public static String toString(long i, int radix) { 126 if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) 127 radix = 10; 128 if (radix == 10) 129 return toString(i); 130 131 if (COMPACT_STRINGS) { 132 byte[] buf = new byte[65]; 133 int charPos = 64; 134 boolean negative = (i < 0); 135 136 if (!negative) { 137 i = -i; 138 } 139 140 while (i <= -radix) { 141 buf[charPos--] = (byte)Integer.digits[(int)(-(i % radix))]; 142 i = i / radix; 143 } 144 buf[charPos] = (byte)Integer.digits[(int)(-i)]; 145 146 if (negative) { 147 buf[--charPos] = '-'; 148 } 149 return StringLatin1.newString(buf, charPos, (65 - charPos)); 150 } 151 return toStringUTF16(i, radix); 152 } 153 154 private static String toStringUTF16(long i, int radix) { 155 byte[] buf = new byte[65 * 2]; 156 int charPos = 64; 157 boolean negative = (i < 0); 158 if (!negative) { 159 i = -i; 160 } 161 while (i <= -radix) { 162 StringUTF16.putChar(buf, charPos--, Integer.digits[(int)(-(i % radix))]); 163 i = i / radix; 164 } 165 StringUTF16.putChar(buf, charPos, Integer.digits[(int)(-i)]); 166 if (negative) { 167 StringUTF16.putChar(buf, --charPos, '-'); 168 } 169 return StringUTF16.newString(buf, charPos, (65 - charPos)); 170 } 171 172 /** 173 * Returns a string representation of the first argument as an 174 * unsigned integer value in the radix specified by the second 175 * argument. 176 * 177 * <p>If the radix is smaller than {@code Character.MIN_RADIX} 178 * or larger than {@code Character.MAX_RADIX}, then the radix 179 * {@code 10} is used instead. 180 * 181 * <p>Note that since the first argument is treated as an unsigned 182 * value, no leading sign character is printed. 183 * 184 * <p>If the magnitude is zero, it is represented by a single zero 185 * character {@code '0'} ({@code '\u005Cu0030'}); otherwise, 186 * the first character of the representation of the magnitude will 187 * not be the zero character. 188 * 189 * <p>The behavior of radixes and the characters used as digits 190 * are the same as {@link #toString(long, int) toString}. 191 * 192 * @param i an integer to be converted to an unsigned string. 193 * @param radix the radix to use in the string representation. 194 * @return an unsigned string representation of the argument in the specified radix. 195 * @see #toString(long, int) 196 * @since 1.8 197 */ 198 public static String toUnsignedString(long i, int radix) { 199 if (i >= 0) 200 return toString(i, radix); 201 else { 202 switch (radix) { 203 case 2: 204 return toBinaryString(i); 205 206 case 4: 207 return toUnsignedString0(i, 2); 208 209 case 8: 210 return toOctalString(i); 211 212 case 10: 213 /* 214 * We can get the effect of an unsigned division by 10 215 * on a long value by first shifting right, yielding a 216 * positive value, and then dividing by 5. This 217 * allows the last digit and preceding digits to be 218 * isolated more quickly than by an initial conversion 219 * to BigInteger. 220 */ 221 long quot = (i >>> 1) / 5; 222 long rem = i - quot * 10; 223 return toString(quot) + rem; 224 225 case 16: 226 return toHexString(i); 227 228 case 32: 229 return toUnsignedString0(i, 5); 230 231 default: 232 return toUnsignedBigInteger(i).toString(radix); 233 } 234 } 235 } 236 237 /** 238 * Return a BigInteger equal to the unsigned value of the 239 * argument. 240 */ 241 private static BigInteger toUnsignedBigInteger(long i) { 242 if (i >= 0L) 243 return BigInteger.valueOf(i); 244 else { 245 int upper = (int) (i >>> 32); 246 int lower = (int) i; 247 248 // return (upper << 32) + lower 249 return (BigInteger.valueOf(Integer.toUnsignedLong(upper))).shiftLeft(32). 250 add(BigInteger.valueOf(Integer.toUnsignedLong(lower))); 251 } 252 } 253 254 /** 255 * Returns a string representation of the {@code long} 256 * argument as an unsigned integer in base 16. 257 * 258 * <p>The unsigned {@code long} value is the argument plus 259 * 2<sup>64</sup> if the argument is negative; otherwise, it is 260 * equal to the argument. This value is converted to a string of 261 * ASCII digits in hexadecimal (base 16) with no extra 262 * leading {@code 0}s. 263 * 264 * <p>The value of the argument can be recovered from the returned 265 * string {@code s} by calling {@link 266 * Long#parseUnsignedLong(String, int) Long.parseUnsignedLong(s, 267 * 16)}. 268 * 269 * <p>If the unsigned magnitude is zero, it is represented by a 270 * single zero character {@code '0'} ({@code '\u005Cu0030'}); 271 * otherwise, the first character of the representation of the 272 * unsigned magnitude will not be the zero character. The 273 * following characters are used as hexadecimal digits: 274 * 275 * <blockquote> 276 * {@code 0123456789abcdef} 277 * </blockquote> 278 * 279 * These are the characters {@code '\u005Cu0030'} through 280 * {@code '\u005Cu0039'} and {@code '\u005Cu0061'} through 281 * {@code '\u005Cu0066'}. If uppercase letters are desired, 282 * the {@link java.lang.String#toUpperCase()} method may be called 283 * on the result: 284 * 285 * <blockquote> 286 * {@code Long.toHexString(n).toUpperCase()} 287 * </blockquote> 288 * 289 * @param i a {@code long} to be converted to a string. 290 * @return the string representation of the unsigned {@code long} 291 * value represented by the argument in hexadecimal 292 * (base 16). 293 * @see #parseUnsignedLong(String, int) 294 * @see #toUnsignedString(long, int) 295 * @since 1.0.2 296 */ 297 public static String toHexString(long i) { 298 return toUnsignedString0(i, 4); 299 } 300 301 /** 302 * Returns a string representation of the {@code long} 303 * argument as an unsigned integer in base 8. 304 * 305 * <p>The unsigned {@code long} value is the argument plus 306 * 2<sup>64</sup> if the argument is negative; otherwise, it is 307 * equal to the argument. This value is converted to a string of 308 * ASCII digits in octal (base 8) with no extra leading 309 * {@code 0}s. 310 * 311 * <p>The value of the argument can be recovered from the returned 312 * string {@code s} by calling {@link 313 * Long#parseUnsignedLong(String, int) Long.parseUnsignedLong(s, 314 * 8)}. 315 * 316 * <p>If the unsigned magnitude is zero, it is represented by a 317 * single zero character {@code '0'} ({@code '\u005Cu0030'}); 318 * otherwise, the first character of the representation of the 319 * unsigned magnitude will not be the zero character. The 320 * following characters are used as octal digits: 321 * 322 * <blockquote> 323 * {@code 01234567} 324 * </blockquote> 325 * 326 * These are the characters {@code '\u005Cu0030'} through 327 * {@code '\u005Cu0037'}. 328 * 329 * @param i a {@code long} to be converted to a string. 330 * @return the string representation of the unsigned {@code long} 331 * value represented by the argument in octal (base 8). 332 * @see #parseUnsignedLong(String, int) 333 * @see #toUnsignedString(long, int) 334 * @since 1.0.2 335 */ 336 public static String toOctalString(long i) { 337 return toUnsignedString0(i, 3); 338 } 339 340 /** 341 * Returns a string representation of the {@code long} 342 * argument as an unsigned integer in base 2. 343 * 344 * <p>The unsigned {@code long} value is the argument plus 345 * 2<sup>64</sup> if the argument is negative; otherwise, it is 346 * equal to the argument. This value is converted to a string of 347 * ASCII digits in binary (base 2) with no extra leading 348 * {@code 0}s. 349 * 350 * <p>The value of the argument can be recovered from the returned 351 * string {@code s} by calling {@link 352 * Long#parseUnsignedLong(String, int) Long.parseUnsignedLong(s, 353 * 2)}. 354 * 355 * <p>If the unsigned magnitude is zero, it is represented by a 356 * single zero character {@code '0'} ({@code '\u005Cu0030'}); 357 * otherwise, the first character of the representation of the 358 * unsigned magnitude will not be the zero character. The 359 * characters {@code '0'} ({@code '\u005Cu0030'}) and {@code 360 * '1'} ({@code '\u005Cu0031'}) are used as binary digits. 361 * 362 * @param i a {@code long} to be converted to a string. 363 * @return the string representation of the unsigned {@code long} 364 * value represented by the argument in binary (base 2). 365 * @see #parseUnsignedLong(String, int) 366 * @see #toUnsignedString(long, int) 367 * @since 1.0.2 368 */ 369 public static String toBinaryString(long i) { 370 return toUnsignedString0(i, 1); 371 } 372 373 /** 374 * Format a long (treated as unsigned) into a String. 375 * @param val the value to format 376 * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary) 377 */ 378 static String toUnsignedString0(long val, int shift) { 379 // assert shift > 0 && shift <=5 : "Illegal shift value"; 380 int mag = Long.SIZE - Long.numberOfLeadingZeros(val); 381 int chars = Math.max(((mag + (shift - 1)) / shift), 1); 382 if (COMPACT_STRINGS) { 383 byte[] buf = new byte[chars]; 384 formatUnsignedLong0(val, shift, buf, 0, chars); 385 return new String(buf, LATIN1); 386 } else { 387 byte[] buf = new byte[chars * 2]; 388 formatUnsignedLong0UTF16(val, shift, buf, 0, chars); 389 return new String(buf, UTF16); 390 } 391 } 392 393 /** 394 * Format a long (treated as unsigned) into a character buffer. If 395 * {@code len} exceeds the formatted ASCII representation of {@code val}, 396 * {@code buf} will be padded with leading zeroes. 397 * 398 * @param val the unsigned long to format 399 * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary) 400 * @param buf the character buffer to write to 401 * @param offset the offset in the destination buffer to start at 402 * @param len the number of characters to write 403 */ 404 405 /** byte[]/LATIN1 version */ 406 static void formatUnsignedLong0(long val, int shift, byte[] buf, int offset, int len) { 407 int charPos = offset + len; 408 int radix = 1 << shift; 409 int mask = radix - 1; 410 do { 411 buf[--charPos] = (byte)Integer.digits[((int) val) & mask]; 412 val >>>= shift; 413 } while (charPos > offset); 414 } 415 416 /** byte[]/UTF16 version */ 417 private static void formatUnsignedLong0UTF16(long val, int shift, byte[] buf, int offset, int len) { 418 int charPos = offset + len; 419 int radix = 1 << shift; 420 int mask = radix - 1; 421 do { 422 StringUTF16.putChar(buf, --charPos, Integer.digits[((int) val) & mask]); 423 val >>>= shift; 424 } while (charPos > offset); 425 } 426 427 static String fastUUID(long lsb, long msb) { 428 if (COMPACT_STRINGS) { 429 byte[] buf = new byte[36]; 430 formatUnsignedLong0(lsb, 4, buf, 24, 12); 431 formatUnsignedLong0(lsb >>> 48, 4, buf, 19, 4); 432 formatUnsignedLong0(msb, 4, buf, 14, 4); 433 formatUnsignedLong0(msb >>> 16, 4, buf, 9, 4); 434 formatUnsignedLong0(msb >>> 32, 4, buf, 0, 8); 435 436 buf[23] = '-'; 437 buf[18] = '-'; 438 buf[13] = '-'; 439 buf[8] = '-'; 440 441 return new String(buf, LATIN1); 442 } else { 443 byte[] buf = new byte[72]; 444 445 formatUnsignedLong0UTF16(lsb, 4, buf, 24, 12); 446 formatUnsignedLong0UTF16(lsb >>> 48, 4, buf, 19, 4); 447 formatUnsignedLong0UTF16(msb, 4, buf, 14, 4); 448 formatUnsignedLong0UTF16(msb >>> 16, 4, buf, 9, 4); 449 formatUnsignedLong0UTF16(msb >>> 32, 4, buf, 0, 8); 450 451 StringUTF16.putChar(buf, 23, '-'); 452 StringUTF16.putChar(buf, 18, '-'); 453 StringUTF16.putChar(buf, 13, '-'); 454 StringUTF16.putChar(buf, 8, '-'); 455 456 return new String(buf, UTF16); 457 } 458 } 459 460 /** 461 * Returns a {@code String} object representing the specified 462 * {@code long}. The argument is converted to signed decimal 463 * representation and returned as a string, exactly as if the 464 * argument and the radix 10 were given as arguments to the {@link 465 * #toString(long, int)} method. 466 * 467 * @param i a {@code long} to be converted. 468 * @return a string representation of the argument in base 10. 469 */ 470 public static String toString(long i) { 471 int size = stringSize(i); 472 if (COMPACT_STRINGS) { 473 byte[] buf = new byte[size]; 474 getChars(i, size, buf); 475 return new String(buf, LATIN1); 476 } else { 477 byte[] buf = new byte[size * 2]; 478 StringUTF16.getChars(i, size, buf); 479 return new String(buf, UTF16); 480 } 481 } 482 483 /** 484 * Returns a string representation of the argument as an unsigned 485 * decimal value. 486 * 487 * The argument is converted to unsigned decimal representation 488 * and returned as a string exactly as if the argument and radix 489 * 10 were given as arguments to the {@link #toUnsignedString(long, 490 * int)} method. 491 * 492 * @param i an integer to be converted to an unsigned string. 493 * @return an unsigned string representation of the argument. 494 * @see #toUnsignedString(long, int) 495 * @since 1.8 496 */ 497 public static String toUnsignedString(long i) { 498 return toUnsignedString(i, 10); 499 } 500 501 /** 502 * Places characters representing the long i into the 503 * character array buf. The characters are placed into 504 * the buffer backwards starting with the least significant 505 * digit at the specified index (exclusive), and working 506 * backwards from there. 507 * 508 * @implNote This method converts positive inputs into negative 509 * values, to cover the Long.MIN_VALUE case. Converting otherwise 510 * (negative to positive) will expose -Long.MIN_VALUE that overflows 511 * long. 512 * 513 * @param i value to convert 514 * @param index next index, after the least significant digit 515 * @param buf target buffer, Latin1-encoded 516 * @return index of the most significant digit or minus sign, if present 517 */ 518 static int getChars(long i, int index, byte[] buf) { 519 long q; 520 int r; 521 int charPos = index; 522 523 boolean negative = (i < 0); 524 if (!negative) { 525 i = -i; 526 } 527 528 // Get 2 digits/iteration using longs until quotient fits into an int 529 while (i <= Integer.MIN_VALUE) { 530 q = i / 100; 531 r = (int)((q * 100) - i); 532 i = q; 533 buf[--charPos] = Integer.DigitOnes[r]; 534 buf[--charPos] = Integer.DigitTens[r]; 535 } 536 537 // Get 2 digits/iteration using ints 538 int q2; 539 int i2 = (int)i; 540 while (i2 <= -100) { 541 q2 = i2 / 100; 542 r = (q2 * 100) - i2; 543 i2 = q2; 544 buf[--charPos] = Integer.DigitOnes[r]; 545 buf[--charPos] = Integer.DigitTens[r]; 546 } 547 548 // We know there are at most two digits left at this point. 549 q2 = i2 / 10; 550 r = (q2 * 10) - i2; 551 buf[--charPos] = (byte)('0' + r); 552 553 // Whatever left is the remaining digit. 554 if (q2 < 0) { 555 buf[--charPos] = (byte)('0' - q2); 556 } 557 558 if (negative) { 559 buf[--charPos] = (byte)'-'; 560 } 561 return charPos; 562 } 563 564 /** 565 * Returns the string representation size for a given long value. 566 * 567 * @param x long value 568 * @return string size 569 * 570 * @implNote There are other ways to compute this: e.g. binary search, 571 * but values are biased heavily towards zero, and therefore linear search 572 * wins. The iteration results are also routinely inlined in the generated 573 * code after loop unrolling. 574 */ 575 static int stringSize(long x) { 576 int d = 1; 577 if (x >= 0) { 578 d = 0; 579 x = -x; 580 } 581 long p = -10; 582 for (int i = 1; i < 19; i++) { 583 if (x > p) 584 return i + d; 585 p = 10 * p; 586 } 587 return 19 + d; 588 } 589 590 /** 591 * Parses the string argument as a signed {@code long} in the 592 * radix specified by the second argument. The characters in the 593 * string must all be digits of the specified radix (as determined 594 * by whether {@link java.lang.Character#digit(char, int)} returns 595 * a nonnegative value), except that the first character may be an 596 * ASCII minus sign {@code '-'} ({@code '\u005Cu002D'}) to 597 * indicate a negative value or an ASCII plus sign {@code '+'} 598 * ({@code '\u005Cu002B'}) to indicate a positive value. The 599 * resulting {@code long} value is returned. 600 * 601 * <p>Note that neither the character {@code L} 602 * ({@code '\u005Cu004C'}) nor {@code l} 603 * ({@code '\u005Cu006C'}) is permitted to appear at the end 604 * of the string as a type indicator, as would be permitted in 605 * Java programming language source code - except that either 606 * {@code L} or {@code l} may appear as a digit for a 607 * radix greater than or equal to 22. 608 * 609 * <p>An exception of type {@code NumberFormatException} is 610 * thrown if any of the following situations occurs: 611 * <ul> 612 * 613 * <li>The first argument is {@code null} or is a string of 614 * length zero. 615 * 616 * <li>The {@code radix} is either smaller than {@link 617 * java.lang.Character#MIN_RADIX} or larger than {@link 618 * java.lang.Character#MAX_RADIX}. 619 * 620 * <li>Any character of the string is not a digit of the specified 621 * radix, except that the first character may be a minus sign 622 * {@code '-'} ({@code '\u005Cu002d'}) or plus sign {@code 623 * '+'} ({@code '\u005Cu002B'}) provided that the string is 624 * longer than length 1. 625 * 626 * <li>The value represented by the string is not a value of type 627 * {@code long}. 628 * </ul> 629 * 630 * <p>Examples: 631 * <blockquote><pre> 632 * parseLong("0", 10) returns 0L 633 * parseLong("473", 10) returns 473L 634 * parseLong("+42", 10) returns 42L 635 * parseLong("-0", 10) returns 0L 636 * parseLong("-FF", 16) returns -255L 637 * parseLong("1100110", 2) returns 102L 638 * parseLong("99", 8) throws a NumberFormatException 639 * parseLong("Hazelnut", 10) throws a NumberFormatException 640 * parseLong("Hazelnut", 36) returns 1356099454469L 641 * </pre></blockquote> 642 * 643 * @param s the {@code String} containing the 644 * {@code long} representation to be parsed. 645 * @param radix the radix to be used while parsing {@code s}. 646 * @return the {@code long} represented by the string argument in 647 * the specified radix. 648 * @throws NumberFormatException if the string does not contain a 649 * parsable {@code long}. 650 */ 651 public static long parseLong(String s, int radix) 652 throws NumberFormatException 653 { 654 if (s == null) { 655 throw new NumberFormatException("null"); 656 } 657 658 if (radix < Character.MIN_RADIX) { 659 throw new NumberFormatException("radix " + radix + 660 " less than Character.MIN_RADIX"); 661 } 662 if (radix > Character.MAX_RADIX) { 663 throw new NumberFormatException("radix " + radix + 664 " greater than Character.MAX_RADIX"); 665 } 666 667 boolean negative = false; 668 int i = 0, len = s.length(); 669 long limit = -Long.MAX_VALUE; 670 671 if (len > 0) { 672 char firstChar = s.charAt(0); 673 if (firstChar < '0') { // Possible leading "+" or "-" 674 if (firstChar == '-') { 675 negative = true; 676 limit = Long.MIN_VALUE; 677 } else if (firstChar != '+') { 678 throw NumberFormatException.forInputString(s); 679 } 680 681 if (len == 1) { // Cannot have lone "+" or "-" 682 throw NumberFormatException.forInputString(s); 683 } 684 i++; 685 } 686 long multmin = limit / radix; 687 long result = 0; 688 while (i < len) { 689 // Accumulating negatively avoids surprises near MAX_VALUE 690 int digit = Character.digit(s.charAt(i++),radix); 691 if (digit < 0 || result < multmin) { 692 throw NumberFormatException.forInputString(s); 693 } 694 result *= radix; 695 if (result < limit + digit) { 696 throw NumberFormatException.forInputString(s); 697 } 698 result -= digit; 699 } 700 return negative ? result : -result; 701 } else { 702 throw NumberFormatException.forInputString(s); 703 } 704 } 705 706 /** 707 * Parses the {@link CharSequence} argument as a signed {@code long} in 708 * the specified {@code radix}, beginning at the specified 709 * {@code beginIndex} and extending to {@code endIndex - 1}. 710 * 711 * <p>The method does not take steps to guard against the 712 * {@code CharSequence} being mutated while parsing. 713 * 714 * @param s the {@code CharSequence} containing the {@code long} 715 * representation to be parsed 716 * @param beginIndex the beginning index, inclusive. 717 * @param endIndex the ending index, exclusive. 718 * @param radix the radix to be used while parsing {@code s}. 719 * @return the signed {@code long} represented by the subsequence in 720 * the specified radix. 721 * @throws NullPointerException if {@code s} is null. 722 * @throws IndexOutOfBoundsException if {@code beginIndex} is 723 * negative, or if {@code beginIndex} is greater than 724 * {@code endIndex} or if {@code endIndex} is greater than 725 * {@code s.length()}. 726 * @throws NumberFormatException if the {@code CharSequence} does not 727 * contain a parsable {@code int} in the specified 728 * {@code radix}, or if {@code radix} is either smaller than 729 * {@link java.lang.Character#MIN_RADIX} or larger than 730 * {@link java.lang.Character#MAX_RADIX}. 731 * @since 9 732 */ 733 public static long parseLong(CharSequence s, int beginIndex, int endIndex, int radix) 734 throws NumberFormatException { 735 s = Objects.requireNonNull(s); 736 737 if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) { 738 throw new IndexOutOfBoundsException(); 739 } 740 if (radix < Character.MIN_RADIX) { 741 throw new NumberFormatException("radix " + radix + 742 " less than Character.MIN_RADIX"); 743 } 744 if (radix > Character.MAX_RADIX) { 745 throw new NumberFormatException("radix " + radix + 746 " greater than Character.MAX_RADIX"); 747 } 748 749 boolean negative = false; 750 int i = beginIndex; 751 long limit = -Long.MAX_VALUE; 752 753 if (i < endIndex) { 754 char firstChar = s.charAt(i); 755 if (firstChar < '0') { // Possible leading "+" or "-" 756 if (firstChar == '-') { 757 negative = true; 758 limit = Long.MIN_VALUE; 759 } else if (firstChar != '+') { 760 throw NumberFormatException.forCharSequence(s, beginIndex, 761 endIndex, i); 762 } 763 i++; 764 } 765 if (i >= endIndex) { // Cannot have lone "+", "-" or "" 766 throw NumberFormatException.forCharSequence(s, beginIndex, 767 endIndex, i); 768 } 769 long multmin = limit / radix; 770 long result = 0; 771 while (i < endIndex) { 772 // Accumulating negatively avoids surprises near MAX_VALUE 773 int digit = Character.digit(s.charAt(i), radix); 774 if (digit < 0 || result < multmin) { 775 throw NumberFormatException.forCharSequence(s, beginIndex, 776 endIndex, i); 777 } 778 result *= radix; 779 if (result < limit + digit) { 780 throw NumberFormatException.forCharSequence(s, beginIndex, 781 endIndex, i); 782 } 783 i++; 784 result -= digit; 785 } 786 return negative ? result : -result; 787 } else { 788 throw new NumberFormatException(""); 789 } 790 } 791 792 /** 793 * Parses the string argument as a signed decimal {@code long}. 794 * The characters in the string must all be decimal digits, except 795 * that the first character may be an ASCII minus sign {@code '-'} 796 * ({@code \u005Cu002D'}) to indicate a negative value or an 797 * ASCII plus sign {@code '+'} ({@code '\u005Cu002B'}) to 798 * indicate a positive value. The resulting {@code long} value is 799 * returned, exactly as if the argument and the radix {@code 10} 800 * were given as arguments to the {@link 801 * #parseLong(java.lang.String, int)} method. 802 * 803 * <p>Note that neither the character {@code L} 804 * ({@code '\u005Cu004C'}) nor {@code l} 805 * ({@code '\u005Cu006C'}) is permitted to appear at the end 806 * of the string as a type indicator, as would be permitted in 807 * Java programming language source code. 808 * 809 * @param s a {@code String} containing the {@code long} 810 * representation to be parsed 811 * @return the {@code long} represented by the argument in 812 * decimal. 813 * @throws NumberFormatException if the string does not contain a 814 * parsable {@code long}. 815 */ 816 public static long parseLong(String s) throws NumberFormatException { 817 return parseLong(s, 10); 818 } 819 820 /** 821 * Parses the string argument as an unsigned {@code long} in the 822 * radix specified by the second argument. An unsigned integer 823 * maps the values usually associated with negative numbers to 824 * positive numbers larger than {@code MAX_VALUE}. 825 * 826 * The characters in the string must all be digits of the 827 * specified radix (as determined by whether {@link 828 * java.lang.Character#digit(char, int)} returns a nonnegative 829 * value), except that the first character may be an ASCII plus 830 * sign {@code '+'} ({@code '\u005Cu002B'}). The resulting 831 * integer value is returned. 832 * 833 * <p>An exception of type {@code NumberFormatException} is 834 * thrown if any of the following situations occurs: 835 * <ul> 836 * <li>The first argument is {@code null} or is a string of 837 * length zero. 838 * 839 * <li>The radix is either smaller than 840 * {@link java.lang.Character#MIN_RADIX} or 841 * larger than {@link java.lang.Character#MAX_RADIX}. 842 * 843 * <li>Any character of the string is not a digit of the specified 844 * radix, except that the first character may be a plus sign 845 * {@code '+'} ({@code '\u005Cu002B'}) provided that the 846 * string is longer than length 1. 847 * 848 * <li>The value represented by the string is larger than the 849 * largest unsigned {@code long}, 2<sup>64</sup>-1. 850 * 851 * </ul> 852 * 853 * 854 * @param s the {@code String} containing the unsigned integer 855 * representation to be parsed 856 * @param radix the radix to be used while parsing {@code s}. 857 * @return the unsigned {@code long} represented by the string 858 * argument in the specified radix. 859 * @throws NumberFormatException if the {@code String} 860 * does not contain a parsable {@code long}. 861 * @since 1.8 862 */ 863 public static long parseUnsignedLong(String s, int radix) 864 throws NumberFormatException { 865 if (s == null) { 866 throw new NumberFormatException("null"); 867 } 868 869 int len = s.length(); 870 if (len > 0) { 871 char firstChar = s.charAt(0); 872 if (firstChar == '-') { 873 throw new 874 NumberFormatException(String.format("Illegal leading minus sign " + 875 "on unsigned string %s.", s)); 876 } else { 877 if (len <= 12 || // Long.MAX_VALUE in Character.MAX_RADIX is 13 digits 878 (radix == 10 && len <= 18) ) { // Long.MAX_VALUE in base 10 is 19 digits 879 return parseLong(s, radix); 880 } 881 882 // No need for range checks on len due to testing above. 883 long first = parseLong(s, 0, len - 1, radix); 884 int second = Character.digit(s.charAt(len - 1), radix); 885 if (second < 0) { 886 throw new NumberFormatException("Bad digit at end of " + s); 887 } 888 long result = first * radix + second; 889 890 /* 891 * Test leftmost bits of multiprecision extension of first*radix 892 * for overflow. The number of bits needed is defined by 893 * GUARD_BIT = ceil(log2(Character.MAX_RADIX)) + 1 = 7. Then 894 * int guard = radix*(int)(first >>> (64 - GUARD_BIT)) and 895 * overflow is tested by splitting guard in the ranges 896 * guard < 92, 92 <= guard < 128, and 128 <= guard, where 897 * 92 = 128 - Character.MAX_RADIX. Note that guard cannot take 898 * on a value which does not include a prime factor in the legal 899 * radix range. 900 */ 901 int guard = radix * (int) (first >>> 57); 902 if (guard >= 128 || 903 (result >= 0 && guard >= 128 - Character.MAX_RADIX)) { 904 /* 905 * For purposes of exposition, the programmatic statements 906 * below should be taken to be multi-precision, i.e., not 907 * subject to overflow. 908 * 909 * A) Condition guard >= 128: 910 * If guard >= 128 then first*radix >= 2^7 * 2^57 = 2^64 911 * hence always overflow. 912 * 913 * B) Condition guard < 92: 914 * Define left7 = first >>> 57. 915 * Given first = (left7 * 2^57) + (first & (2^57 - 1)) then 916 * result <= (radix*left7)*2^57 + radix*(2^57 - 1) + second. 917 * Thus if radix*left7 < 92, radix <= 36, and second < 36, 918 * then result < 92*2^57 + 36*(2^57 - 1) + 36 = 2^64 hence 919 * never overflow. 920 * 921 * C) Condition 92 <= guard < 128: 922 * first*radix + second >= radix*left7*2^57 + second 923 * so that first*radix + second >= 92*2^57 + 0 > 2^63 924 * 925 * D) Condition guard < 128: 926 * radix*first <= (radix*left7) * 2^57 + radix*(2^57 - 1) 927 * so 928 * radix*first + second <= (radix*left7) * 2^57 + radix*(2^57 - 1) + 36 929 * thus 930 * radix*first + second < 128 * 2^57 + 36*2^57 - radix + 36 931 * whence 932 * radix*first + second < 2^64 + 2^6*2^57 = 2^64 + 2^63 933 * 934 * E) Conditions C, D, and result >= 0: 935 * C and D combined imply the mathematical result 936 * 2^63 < first*radix + second < 2^64 + 2^63. The lower 937 * bound is therefore negative as a signed long, but the 938 * upper bound is too small to overflow again after the 939 * signed long overflows to positive above 2^64 - 1. Hence 940 * result >= 0 implies overflow given C and D. 941 */ 942 throw new NumberFormatException(String.format("String value %s exceeds " + 943 "range of unsigned long.", s)); 944 } 945 return result; 946 } 947 } else { 948 throw NumberFormatException.forInputString(s); 949 } 950 } 951 952 /** 953 * Parses the {@link CharSequence} argument as an unsigned {@code long} in 954 * the specified {@code radix}, beginning at the specified 955 * {@code beginIndex} and extending to {@code endIndex - 1}. 956 * 957 * <p>The method does not take steps to guard against the 958 * {@code CharSequence} being mutated while parsing. 959 * 960 * @param s the {@code CharSequence} containing the unsigned 961 * {@code long} representation to be parsed 962 * @param beginIndex the beginning index, inclusive. 963 * @param endIndex the ending index, exclusive. 964 * @param radix the radix to be used while parsing {@code s}. 965 * @return the unsigned {@code long} represented by the subsequence in 966 * the specified radix. 967 * @throws NullPointerException if {@code s} is null. 968 * @throws IndexOutOfBoundsException if {@code beginIndex} is 969 * negative, or if {@code beginIndex} is greater than 970 * {@code endIndex} or if {@code endIndex} is greater than 971 * {@code s.length()}. 972 * @throws NumberFormatException if the {@code CharSequence} does not 973 * contain a parsable unsigned {@code long} in the specified 974 * {@code radix}, or if {@code radix} is either smaller than 975 * {@link java.lang.Character#MIN_RADIX} or larger than 976 * {@link java.lang.Character#MAX_RADIX}. 977 * @since 9 978 */ 979 public static long parseUnsignedLong(CharSequence s, int beginIndex, int endIndex, int radix) 980 throws NumberFormatException { 981 s = Objects.requireNonNull(s); 982 983 if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) { 984 throw new IndexOutOfBoundsException(); 985 } 986 int start = beginIndex, len = endIndex - beginIndex; 987 988 if (len > 0) { 989 char firstChar = s.charAt(start); 990 if (firstChar == '-') { 991 throw new NumberFormatException(String.format("Illegal leading minus sign " + 992 "on unsigned string %s.", s.subSequence(start, start + len))); 993 } else { 994 if (len <= 12 || // Long.MAX_VALUE in Character.MAX_RADIX is 13 digits 995 (radix == 10 && len <= 18) ) { // Long.MAX_VALUE in base 10 is 19 digits 996 return parseLong(s, start, start + len, radix); 997 } 998 999 // No need for range checks on end due to testing above. 1000 long first = parseLong(s, start, start + len - 1, radix); 1001 int second = Character.digit(s.charAt(start + len - 1), radix); 1002 if (second < 0) { 1003 throw new NumberFormatException("Bad digit at end of " + 1004 s.subSequence(start, start + len)); 1005 } 1006 long result = first * radix + second; 1007 1008 /* 1009 * Test leftmost bits of multiprecision extension of first*radix 1010 * for overflow. The number of bits needed is defined by 1011 * GUARD_BIT = ceil(log2(Character.MAX_RADIX)) + 1 = 7. Then 1012 * int guard = radix*(int)(first >>> (64 - GUARD_BIT)) and 1013 * overflow is tested by splitting guard in the ranges 1014 * guard < 92, 92 <= guard < 128, and 128 <= guard, where 1015 * 92 = 128 - Character.MAX_RADIX. Note that guard cannot take 1016 * on a value which does not include a prime factor in the legal 1017 * radix range. 1018 */ 1019 int guard = radix * (int) (first >>> 57); 1020 if (guard >= 128 || 1021 (result >= 0 && guard >= 128 - Character.MAX_RADIX)) { 1022 /* 1023 * For purposes of exposition, the programmatic statements 1024 * below should be taken to be multi-precision, i.e., not 1025 * subject to overflow. 1026 * 1027 * A) Condition guard >= 128: 1028 * If guard >= 128 then first*radix >= 2^7 * 2^57 = 2^64 1029 * hence always overflow. 1030 * 1031 * B) Condition guard < 92: 1032 * Define left7 = first >>> 57. 1033 * Given first = (left7 * 2^57) + (first & (2^57 - 1)) then 1034 * result <= (radix*left7)*2^57 + radix*(2^57 - 1) + second. 1035 * Thus if radix*left7 < 92, radix <= 36, and second < 36, 1036 * then result < 92*2^57 + 36*(2^57 - 1) + 36 = 2^64 hence 1037 * never overflow. 1038 * 1039 * C) Condition 92 <= guard < 128: 1040 * first*radix + second >= radix*left7*2^57 + second 1041 * so that first*radix + second >= 92*2^57 + 0 > 2^63 1042 * 1043 * D) Condition guard < 128: 1044 * radix*first <= (radix*left7) * 2^57 + radix*(2^57 - 1) 1045 * so 1046 * radix*first + second <= (radix*left7) * 2^57 + radix*(2^57 - 1) + 36 1047 * thus 1048 * radix*first + second < 128 * 2^57 + 36*2^57 - radix + 36 1049 * whence 1050 * radix*first + second < 2^64 + 2^6*2^57 = 2^64 + 2^63 1051 * 1052 * E) Conditions C, D, and result >= 0: 1053 * C and D combined imply the mathematical result 1054 * 2^63 < first*radix + second < 2^64 + 2^63. The lower 1055 * bound is therefore negative as a signed long, but the 1056 * upper bound is too small to overflow again after the 1057 * signed long overflows to positive above 2^64 - 1. Hence 1058 * result >= 0 implies overflow given C and D. 1059 */ 1060 throw new NumberFormatException(String.format("String value %s exceeds " + 1061 "range of unsigned long.", s.subSequence(start, start + len))); 1062 } 1063 return result; 1064 } 1065 } else { 1066 throw NumberFormatException.forInputString(""); 1067 } 1068 } 1069 1070 /** 1071 * Parses the string argument as an unsigned decimal {@code long}. The 1072 * characters in the string must all be decimal digits, except 1073 * that the first character may be an ASCII plus sign {@code 1074 * '+'} ({@code '\u005Cu002B'}). The resulting integer value 1075 * is returned, exactly as if the argument and the radix 10 were 1076 * given as arguments to the {@link 1077 * #parseUnsignedLong(java.lang.String, int)} method. 1078 * 1079 * @param s a {@code String} containing the unsigned {@code long} 1080 * representation to be parsed 1081 * @return the unsigned {@code long} value represented by the decimal string argument 1082 * @throws NumberFormatException if the string does not contain a 1083 * parsable unsigned integer. 1084 * @since 1.8 1085 */ 1086 public static long parseUnsignedLong(String s) throws NumberFormatException { 1087 return parseUnsignedLong(s, 10); 1088 } 1089 1090 /** 1091 * Returns a {@code Long} object holding the value 1092 * extracted from the specified {@code String} when parsed 1093 * with the radix given by the second argument. The first 1094 * argument is interpreted as representing a signed 1095 * {@code long} in the radix specified by the second 1096 * argument, exactly as if the arguments were given to the {@link 1097 * #parseLong(java.lang.String, int)} method. The result is a 1098 * {@code Long} object that represents the {@code long} 1099 * value specified by the string. 1100 * 1101 * <p>In other words, this method returns a {@code Long} object equal 1102 * to the value of: 1103 * 1104 * <blockquote> 1105 * {@code new Long(Long.parseLong(s, radix))} 1106 * </blockquote> 1107 * 1108 * @param s the string to be parsed 1109 * @param radix the radix to be used in interpreting {@code s} 1110 * @return a {@code Long} object holding the value 1111 * represented by the string argument in the specified 1112 * radix. 1113 * @throws NumberFormatException If the {@code String} does not 1114 * contain a parsable {@code long}. 1115 */ 1116 public static Long valueOf(String s, int radix) throws NumberFormatException { 1117 return Long.valueOf(parseLong(s, radix)); 1118 } 1119 1120 /** 1121 * Returns a {@code Long} object holding the value 1122 * of the specified {@code String}. The argument is 1123 * interpreted as representing a signed decimal {@code long}, 1124 * exactly as if the argument were given to the {@link 1125 * #parseLong(java.lang.String)} method. The result is a 1126 * {@code Long} object that represents the integer value 1127 * specified by the string. 1128 * 1129 * <p>In other words, this method returns a {@code Long} object 1130 * equal to the value of: 1131 * 1132 * <blockquote> 1133 * {@code new Long(Long.parseLong(s))} 1134 * </blockquote> 1135 * 1136 * @param s the string to be parsed. 1137 * @return a {@code Long} object holding the value 1138 * represented by the string argument. 1139 * @throws NumberFormatException If the string cannot be parsed 1140 * as a {@code long}. 1141 */ 1142 public static Long valueOf(String s) throws NumberFormatException 1143 { 1144 return Long.valueOf(parseLong(s, 10)); 1145 } 1146 1147 private static class LongCache { 1148 private LongCache(){} 1149 1150 static final Long cache[] = new Long[-(-128) + 127 + 1]; 1151 1152 static { 1153 for(int i = 0; i < cache.length; i++) 1154 cache[i] = new Long(i - 128); 1155 } 1156 } 1157 1158 /** 1159 * Returns a {@code Long} instance representing the specified 1160 * {@code long} value. 1161 * If a new {@code Long} instance is not required, this method 1162 * should generally be used in preference to the constructor 1163 * {@link #Long(long)}, as this method is likely to yield 1164 * significantly better space and time performance by caching 1165 * frequently requested values. 1166 * 1167 * Note that unlike the {@linkplain Integer#valueOf(int) 1168 * corresponding method} in the {@code Integer} class, this method 1169 * is <em>not</em> required to cache values within a particular 1170 * range. 1171 * 1172 * @param l a long value. 1173 * @return a {@code Long} instance representing {@code l}. 1174 * @since 1.5 1175 */ 1176 @HotSpotIntrinsicCandidate 1177 public static Long valueOf(long l) { 1178 final int offset = 128; 1179 if (l >= -128 && l <= 127) { // will cache 1180 return LongCache.cache[(int)l + offset]; 1181 } 1182 return new Long(l); 1183 } 1184 1185 /** 1186 * Decodes a {@code String} into a {@code Long}. 1187 * Accepts decimal, hexadecimal, and octal numbers given by the 1188 * following grammar: 1189 * 1190 * <blockquote> 1191 * <dl> 1192 * <dt><i>DecodableString:</i> 1193 * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i> 1194 * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i> 1195 * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i> 1196 * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i> 1197 * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i> 1198 * 1199 * <dt><i>Sign:</i> 1200 * <dd>{@code -} 1201 * <dd>{@code +} 1202 * </dl> 1203 * </blockquote> 1204 * 1205 * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i> 1206 * are as defined in section 3.10.1 of 1207 * <cite>The Java™ Language Specification</cite>, 1208 * except that underscores are not accepted between digits. 1209 * 1210 * <p>The sequence of characters following an optional 1211 * sign and/or radix specifier ("{@code 0x}", "{@code 0X}", 1212 * "{@code #}", or leading zero) is parsed as by the {@code 1213 * Long.parseLong} method with the indicated radix (10, 16, or 8). 1214 * This sequence of characters must represent a positive value or 1215 * a {@link NumberFormatException} will be thrown. The result is 1216 * negated if first character of the specified {@code String} is 1217 * the minus sign. No whitespace characters are permitted in the 1218 * {@code String}. 1219 * 1220 * @param nm the {@code String} to decode. 1221 * @return a {@code Long} object holding the {@code long} 1222 * value represented by {@code nm} 1223 * @throws NumberFormatException if the {@code String} does not 1224 * contain a parsable {@code long}. 1225 * @see java.lang.Long#parseLong(String, int) 1226 * @since 1.2 1227 */ 1228 public static Long decode(String nm) throws NumberFormatException { 1229 int radix = 10; 1230 int index = 0; 1231 boolean negative = false; 1232 Long result; 1233 1234 if (nm.length() == 0) 1235 throw new NumberFormatException("Zero length string"); 1236 char firstChar = nm.charAt(0); 1237 // Handle sign, if present 1238 if (firstChar == '-') { 1239 negative = true; 1240 index++; 1241 } else if (firstChar == '+') 1242 index++; 1243 1244 // Handle radix specifier, if present 1245 if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) { 1246 index += 2; 1247 radix = 16; 1248 } 1249 else if (nm.startsWith("#", index)) { 1250 index ++; 1251 radix = 16; 1252 } 1253 else if (nm.startsWith("0", index) && nm.length() > 1 + index) { 1254 index ++; 1255 radix = 8; 1256 } 1257 1258 if (nm.startsWith("-", index) || nm.startsWith("+", index)) 1259 throw new NumberFormatException("Sign character in wrong position"); 1260 1261 try { 1262 result = Long.valueOf(nm.substring(index), radix); 1263 result = negative ? Long.valueOf(-result.longValue()) : result; 1264 } catch (NumberFormatException e) { 1265 // If number is Long.MIN_VALUE, we'll end up here. The next line 1266 // handles this case, and causes any genuine format error to be 1267 // rethrown. 1268 String constant = negative ? ("-" + nm.substring(index)) 1269 : nm.substring(index); 1270 result = Long.valueOf(constant, radix); 1271 } 1272 return result; 1273 } 1274 1275 /** 1276 * The value of the {@code Long}. 1277 * 1278 * @serial 1279 */ 1280 private final long value; 1281 1282 /** 1283 * Constructs a newly allocated {@code Long} object that 1284 * represents the specified {@code long} argument. 1285 * 1286 * @param value the value to be represented by the 1287 * {@code Long} object. 1288 * 1289 * @deprecated 1290 * It is rarely appropriate to use this constructor. The static factory 1291 * {@link #valueOf(long)} is generally a better choice, as it is 1292 * likely to yield significantly better space and time performance. 1293 */ 1294 @Deprecated(since="9") 1295 public Long(long value) { 1296 this.value = value; 1297 } 1298 1299 /** 1300 * Constructs a newly allocated {@code Long} object that 1301 * represents the {@code long} value indicated by the 1302 * {@code String} parameter. The string is converted to a 1303 * {@code long} value in exactly the manner used by the 1304 * {@code parseLong} method for radix 10. 1305 * 1306 * @param s the {@code String} to be converted to a 1307 * {@code Long}. 1308 * @throws NumberFormatException if the {@code String} does not 1309 * contain a parsable {@code long}. 1310 * 1311 * @deprecated 1312 * It is rarely appropriate to use this constructor. 1313 * Use {@link #parseLong(String)} to convert a string to a 1314 * {@code long} primitive, or use {@link #valueOf(String)} 1315 * to convert a string to a {@code Long} object. 1316 */ 1317 @Deprecated(since="9") 1318 public Long(String s) throws NumberFormatException { 1319 this.value = parseLong(s, 10); 1320 } 1321 1322 /** 1323 * Returns the value of this {@code Long} as a {@code byte} after 1324 * a narrowing primitive conversion. 1325 * @jls 5.1.3 Narrowing Primitive Conversions 1326 */ 1327 public byte byteValue() { 1328 return (byte)value; 1329 } 1330 1331 /** 1332 * Returns the value of this {@code Long} as a {@code short} after 1333 * a narrowing primitive conversion. 1334 * @jls 5.1.3 Narrowing Primitive Conversions 1335 */ 1336 public short shortValue() { 1337 return (short)value; 1338 } 1339 1340 /** 1341 * Returns the value of this {@code Long} as an {@code int} after 1342 * a narrowing primitive conversion. 1343 * @jls 5.1.3 Narrowing Primitive Conversions 1344 */ 1345 public int intValue() { 1346 return (int)value; 1347 } 1348 1349 /** 1350 * Returns the value of this {@code Long} as a 1351 * {@code long} value. 1352 */ 1353 @HotSpotIntrinsicCandidate 1354 public long longValue() { 1355 return value; 1356 } 1357 1358 /** 1359 * Returns the value of this {@code Long} as a {@code float} after 1360 * a widening primitive conversion. 1361 * @jls 5.1.2 Widening Primitive Conversions 1362 */ 1363 public float floatValue() { 1364 return (float)value; 1365 } 1366 1367 /** 1368 * Returns the value of this {@code Long} as a {@code double} 1369 * after a widening primitive conversion. 1370 * @jls 5.1.2 Widening Primitive Conversions 1371 */ 1372 public double doubleValue() { 1373 return (double)value; 1374 } 1375 1376 /** 1377 * Returns a {@code String} object representing this 1378 * {@code Long}'s value. The value is converted to signed 1379 * decimal representation and returned as a string, exactly as if 1380 * the {@code long} value were given as an argument to the 1381 * {@link java.lang.Long#toString(long)} method. 1382 * 1383 * @return a string representation of the value of this object in 1384 * base 10. 1385 */ 1386 public String toString() { 1387 return toString(value); 1388 } 1389 1390 /** 1391 * Returns a hash code for this {@code Long}. The result is 1392 * the exclusive OR of the two halves of the primitive 1393 * {@code long} value held by this {@code Long} 1394 * object. That is, the hashcode is the value of the expression: 1395 * 1396 * <blockquote> 1397 * {@code (int)(this.longValue()^(this.longValue()>>>32))} 1398 * </blockquote> 1399 * 1400 * @return a hash code value for this object. 1401 */ 1402 @Override 1403 public int hashCode() { 1404 return Long.hashCode(value); 1405 } 1406 1407 /** 1408 * Returns a hash code for a {@code long} value; compatible with 1409 * {@code Long.hashCode()}. 1410 * 1411 * @param value the value to hash 1412 * @return a hash code value for a {@code long} value. 1413 * @since 1.8 1414 */ 1415 public static int hashCode(long value) { 1416 return (int)(value ^ (value >>> 32)); 1417 } 1418 1419 /** 1420 * Compares this object to the specified object. The result is 1421 * {@code true} if and only if the argument is not 1422 * {@code null} and is a {@code Long} object that 1423 * contains the same {@code long} value as this object. 1424 * 1425 * @param obj the object to compare with. 1426 * @return {@code true} if the objects are the same; 1427 * {@code false} otherwise. 1428 */ 1429 public boolean equals(Object obj) { 1430 if (obj instanceof Long) { 1431 return value == ((Long)obj).longValue(); 1432 } 1433 return false; 1434 } 1435 1436 /** 1437 * Determines the {@code long} value of the system property 1438 * with the specified name. 1439 * 1440 * <p>The first argument is treated as the name of a system 1441 * property. System properties are accessible through the {@link 1442 * java.lang.System#getProperty(java.lang.String)} method. The 1443 * string value of this property is then interpreted as a {@code 1444 * long} value using the grammar supported by {@link Long#decode decode} 1445 * and a {@code Long} object representing this value is returned. 1446 * 1447 * <p>If there is no property with the specified name, if the 1448 * specified name is empty or {@code null}, or if the property 1449 * does not have the correct numeric format, then {@code null} is 1450 * returned. 1451 * 1452 * <p>In other words, this method returns a {@code Long} object 1453 * equal to the value of: 1454 * 1455 * <blockquote> 1456 * {@code getLong(nm, null)} 1457 * </blockquote> 1458 * 1459 * @param nm property name. 1460 * @return the {@code Long} value of the property. 1461 * @throws SecurityException for the same reasons as 1462 * {@link System#getProperty(String) System.getProperty} 1463 * @see java.lang.System#getProperty(java.lang.String) 1464 * @see java.lang.System#getProperty(java.lang.String, java.lang.String) 1465 */ 1466 public static Long getLong(String nm) { 1467 return getLong(nm, null); 1468 } 1469 1470 /** 1471 * Determines the {@code long} value of the system property 1472 * with the specified name. 1473 * 1474 * <p>The first argument is treated as the name of a system 1475 * property. System properties are accessible through the {@link 1476 * java.lang.System#getProperty(java.lang.String)} method. The 1477 * string value of this property is then interpreted as a {@code 1478 * long} value using the grammar supported by {@link Long#decode decode} 1479 * and a {@code Long} object representing this value is returned. 1480 * 1481 * <p>The second argument is the default value. A {@code Long} object 1482 * that represents the value of the second argument is returned if there 1483 * is no property of the specified name, if the property does not have 1484 * the correct numeric format, or if the specified name is empty or null. 1485 * 1486 * <p>In other words, this method returns a {@code Long} object equal 1487 * to the value of: 1488 * 1489 * <blockquote> 1490 * {@code getLong(nm, new Long(val))} 1491 * </blockquote> 1492 * 1493 * but in practice it may be implemented in a manner such as: 1494 * 1495 * <blockquote><pre> 1496 * Long result = getLong(nm, null); 1497 * return (result == null) ? new Long(val) : result; 1498 * </pre></blockquote> 1499 * 1500 * to avoid the unnecessary allocation of a {@code Long} object when 1501 * the default value is not needed. 1502 * 1503 * @param nm property name. 1504 * @param val default value. 1505 * @return the {@code Long} value of the property. 1506 * @throws SecurityException for the same reasons as 1507 * {@link System#getProperty(String) System.getProperty} 1508 * @see java.lang.System#getProperty(java.lang.String) 1509 * @see java.lang.System#getProperty(java.lang.String, java.lang.String) 1510 */ 1511 public static Long getLong(String nm, long val) { 1512 Long result = Long.getLong(nm, null); 1513 return (result == null) ? Long.valueOf(val) : result; 1514 } 1515 1516 /** 1517 * Returns the {@code long} value of the system property with 1518 * the specified name. The first argument is treated as the name 1519 * of a system property. System properties are accessible through 1520 * the {@link java.lang.System#getProperty(java.lang.String)} 1521 * method. The string value of this property is then interpreted 1522 * as a {@code long} value, as per the 1523 * {@link Long#decode decode} method, and a {@code Long} object 1524 * representing this value is returned; in summary: 1525 * 1526 * <ul> 1527 * <li>If the property value begins with the two ASCII characters 1528 * {@code 0x} or the ASCII character {@code #}, not followed by 1529 * a minus sign, then the rest of it is parsed as a hexadecimal integer 1530 * exactly as for the method {@link #valueOf(java.lang.String, int)} 1531 * with radix 16. 1532 * <li>If the property value begins with the ASCII character 1533 * {@code 0} followed by another character, it is parsed as 1534 * an octal integer exactly as by the method {@link 1535 * #valueOf(java.lang.String, int)} with radix 8. 1536 * <li>Otherwise the property value is parsed as a decimal 1537 * integer exactly as by the method 1538 * {@link #valueOf(java.lang.String, int)} with radix 10. 1539 * </ul> 1540 * 1541 * <p>Note that, in every case, neither {@code L} 1542 * ({@code '\u005Cu004C'}) nor {@code l} 1543 * ({@code '\u005Cu006C'}) is permitted to appear at the end 1544 * of the property value as a type indicator, as would be 1545 * permitted in Java programming language source code. 1546 * 1547 * <p>The second argument is the default value. The default value is 1548 * returned if there is no property of the specified name, if the 1549 * property does not have the correct numeric format, or if the 1550 * specified name is empty or {@code null}. 1551 * 1552 * @param nm property name. 1553 * @param val default value. 1554 * @return the {@code Long} value of the property. 1555 * @throws SecurityException for the same reasons as 1556 * {@link System#getProperty(String) System.getProperty} 1557 * @see System#getProperty(java.lang.String) 1558 * @see System#getProperty(java.lang.String, java.lang.String) 1559 */ 1560 public static Long getLong(String nm, Long val) { 1561 String v = null; 1562 try { 1563 v = System.getProperty(nm); 1564 } catch (IllegalArgumentException | NullPointerException e) { 1565 } 1566 if (v != null) { 1567 try { 1568 return Long.decode(v); 1569 } catch (NumberFormatException e) { 1570 } 1571 } 1572 return val; 1573 } 1574 1575 /** 1576 * Compares two {@code Long} objects numerically. 1577 * 1578 * @param anotherLong the {@code Long} to be compared. 1579 * @return the value {@code 0} if this {@code Long} is 1580 * equal to the argument {@code Long}; a value less than 1581 * {@code 0} if this {@code Long} is numerically less 1582 * than the argument {@code Long}; and a value greater 1583 * than {@code 0} if this {@code Long} is numerically 1584 * greater than the argument {@code Long} (signed 1585 * comparison). 1586 * @since 1.2 1587 */ 1588 public int compareTo(Long anotherLong) { 1589 return compare(this.value, anotherLong.value); 1590 } 1591 1592 /** 1593 * Compares two {@code long} values numerically. 1594 * The value returned is identical to what would be returned by: 1595 * <pre> 1596 * Long.valueOf(x).compareTo(Long.valueOf(y)) 1597 * </pre> 1598 * 1599 * @param x the first {@code long} to compare 1600 * @param y the second {@code long} to compare 1601 * @return the value {@code 0} if {@code x == y}; 1602 * a value less than {@code 0} if {@code x < y}; and 1603 * a value greater than {@code 0} if {@code x > y} 1604 * @since 1.7 1605 */ 1606 public static int compare(long x, long y) { 1607 return (x < y) ? -1 : ((x == y) ? 0 : 1); 1608 } 1609 1610 /** 1611 * Compares two {@code long} values numerically treating the values 1612 * as unsigned. 1613 * 1614 * @param x the first {@code long} to compare 1615 * @param y the second {@code long} to compare 1616 * @return the value {@code 0} if {@code x == y}; a value less 1617 * than {@code 0} if {@code x < y} as unsigned values; and 1618 * a value greater than {@code 0} if {@code x > y} as 1619 * unsigned values 1620 * @since 1.8 1621 */ 1622 public static int compareUnsigned(long x, long y) { 1623 return compare(x + MIN_VALUE, y + MIN_VALUE); 1624 } 1625 1626 1627 /** 1628 * Returns the unsigned quotient of dividing the first argument by 1629 * the second where each argument and the result is interpreted as 1630 * an unsigned value. 1631 * 1632 * <p>Note that in two's complement arithmetic, the three other 1633 * basic arithmetic operations of add, subtract, and multiply are 1634 * bit-wise identical if the two operands are regarded as both 1635 * being signed or both being unsigned. Therefore separate {@code 1636 * addUnsigned}, etc. methods are not provided. 1637 * 1638 * @param dividend the value to be divided 1639 * @param divisor the value doing the dividing 1640 * @return the unsigned quotient of the first argument divided by 1641 * the second argument 1642 * @see #remainderUnsigned 1643 * @since 1.8 1644 */ 1645 public static long divideUnsigned(long dividend, long divisor) { 1646 if (divisor < 0L) { // signed comparison 1647 // Answer must be 0 or 1 depending on relative magnitude 1648 // of dividend and divisor. 1649 return (compareUnsigned(dividend, divisor)) < 0 ? 0L :1L; 1650 } 1651 1652 if (dividend > 0) // Both inputs non-negative 1653 return dividend/divisor; 1654 else { 1655 /* 1656 * For simple code, leveraging BigInteger. Longer and faster 1657 * code written directly in terms of operations on longs is 1658 * possible; see "Hacker's Delight" for divide and remainder 1659 * algorithms. 1660 */ 1661 return toUnsignedBigInteger(dividend). 1662 divide(toUnsignedBigInteger(divisor)).longValue(); 1663 } 1664 } 1665 1666 /** 1667 * Returns the unsigned remainder from dividing the first argument 1668 * by the second where each argument and the result is interpreted 1669 * as an unsigned value. 1670 * 1671 * @param dividend the value to be divided 1672 * @param divisor the value doing the dividing 1673 * @return the unsigned remainder of the first argument divided by 1674 * the second argument 1675 * @see #divideUnsigned 1676 * @since 1.8 1677 */ 1678 public static long remainderUnsigned(long dividend, long divisor) { 1679 if (dividend > 0 && divisor > 0) { // signed comparisons 1680 return dividend % divisor; 1681 } else { 1682 if (compareUnsigned(dividend, divisor) < 0) // Avoid explicit check for 0 divisor 1683 return dividend; 1684 else 1685 return toUnsignedBigInteger(dividend). 1686 remainder(toUnsignedBigInteger(divisor)).longValue(); 1687 } 1688 } 1689 1690 // Bit Twiddling 1691 1692 /** 1693 * The number of bits used to represent a {@code long} value in two's 1694 * complement binary form. 1695 * 1696 * @since 1.5 1697 */ 1698 @Native public static final int SIZE = 64; 1699 1700 /** 1701 * The number of bytes used to represent a {@code long} value in two's 1702 * complement binary form. 1703 * 1704 * @since 1.8 1705 */ 1706 public static final int BYTES = SIZE / Byte.SIZE; 1707 1708 /** 1709 * Returns a {@code long} value with at most a single one-bit, in the 1710 * position of the highest-order ("leftmost") one-bit in the specified 1711 * {@code long} value. Returns zero if the specified value has no 1712 * one-bits in its two's complement binary representation, that is, if it 1713 * is equal to zero. 1714 * 1715 * @param i the value whose highest one bit is to be computed 1716 * @return a {@code long} value with a single one-bit, in the position 1717 * of the highest-order one-bit in the specified value, or zero if 1718 * the specified value is itself equal to zero. 1719 * @since 1.5 1720 */ 1721 public static long highestOneBit(long i) { 1722 // HD, Figure 3-1 1723 i |= (i >> 1); 1724 i |= (i >> 2); 1725 i |= (i >> 4); 1726 i |= (i >> 8); 1727 i |= (i >> 16); 1728 i |= (i >> 32); 1729 return i - (i >>> 1); 1730 } 1731 1732 /** 1733 * Returns a {@code long} value with at most a single one-bit, in the 1734 * position of the lowest-order ("rightmost") one-bit in the specified 1735 * {@code long} value. Returns zero if the specified value has no 1736 * one-bits in its two's complement binary representation, that is, if it 1737 * is equal to zero. 1738 * 1739 * @param i the value whose lowest one bit is to be computed 1740 * @return a {@code long} value with a single one-bit, in the position 1741 * of the lowest-order one-bit in the specified value, or zero if 1742 * the specified value is itself equal to zero. 1743 * @since 1.5 1744 */ 1745 public static long lowestOneBit(long i) { 1746 // HD, Section 2-1 1747 return i & -i; 1748 } 1749 1750 /** 1751 * Returns the number of zero bits preceding the highest-order 1752 * ("leftmost") one-bit in the two's complement binary representation 1753 * of the specified {@code long} value. Returns 64 if the 1754 * specified value has no one-bits in its two's complement representation, 1755 * in other words if it is equal to zero. 1756 * 1757 * <p>Note that this method is closely related to the logarithm base 2. 1758 * For all positive {@code long} values x: 1759 * <ul> 1760 * <li>floor(log<sub>2</sub>(x)) = {@code 63 - numberOfLeadingZeros(x)} 1761 * <li>ceil(log<sub>2</sub>(x)) = {@code 64 - numberOfLeadingZeros(x - 1)} 1762 * </ul> 1763 * 1764 * @param i the value whose number of leading zeros is to be computed 1765 * @return the number of zero bits preceding the highest-order 1766 * ("leftmost") one-bit in the two's complement binary representation 1767 * of the specified {@code long} value, or 64 if the value 1768 * is equal to zero. 1769 * @since 1.5 1770 */ 1771 @HotSpotIntrinsicCandidate 1772 public static int numberOfLeadingZeros(long i) { 1773 // HD, Figure 5-6 1774 if (i <= 0) 1775 return i == 0 ? 64 : 0; 1776 int n = 1; 1777 int x = (int)(i >>> 32); 1778 if (x == 0) { n += 32; x = (int)i; } 1779 if (x >>> 16 == 0) { n += 16; x <<= 16; } 1780 if (x >>> 24 == 0) { n += 8; x <<= 8; } 1781 if (x >>> 28 == 0) { n += 4; x <<= 4; } 1782 if (x >>> 30 == 0) { n += 2; x <<= 2; } 1783 n -= x >>> 31; 1784 return n; 1785 } 1786 1787 /** 1788 * Returns the number of zero bits following the lowest-order ("rightmost") 1789 * one-bit in the two's complement binary representation of the specified 1790 * {@code long} value. Returns 64 if the specified value has no 1791 * one-bits in its two's complement representation, in other words if it is 1792 * equal to zero. 1793 * 1794 * @param i the value whose number of trailing zeros is to be computed 1795 * @return the number of zero bits following the lowest-order ("rightmost") 1796 * one-bit in the two's complement binary representation of the 1797 * specified {@code long} value, or 64 if the value is equal 1798 * to zero. 1799 * @since 1.5 1800 */ 1801 @HotSpotIntrinsicCandidate 1802 public static int numberOfTrailingZeros(long i) { 1803 // HD, Figure 5-14 1804 int x, y; 1805 if (i == 0) return 64; 1806 int n = 63; 1807 y = (int)i; if (y != 0) { n = n -32; x = y; } else x = (int)(i>>>32); 1808 y = x <<16; if (y != 0) { n = n -16; x = y; } 1809 y = x << 8; if (y != 0) { n = n - 8; x = y; } 1810 y = x << 4; if (y != 0) { n = n - 4; x = y; } 1811 y = x << 2; if (y != 0) { n = n - 2; x = y; } 1812 return n - ((x << 1) >>> 31); 1813 } 1814 1815 /** 1816 * Returns the number of one-bits in the two's complement binary 1817 * representation of the specified {@code long} value. This function is 1818 * sometimes referred to as the <i>population count</i>. 1819 * 1820 * @param i the value whose bits are to be counted 1821 * @return the number of one-bits in the two's complement binary 1822 * representation of the specified {@code long} value. 1823 * @since 1.5 1824 */ 1825 @HotSpotIntrinsicCandidate 1826 public static int bitCount(long i) { 1827 // HD, Figure 5-2 1828 i = i - ((i >>> 1) & 0x5555555555555555L); 1829 i = (i & 0x3333333333333333L) + ((i >>> 2) & 0x3333333333333333L); 1830 i = (i + (i >>> 4)) & 0x0f0f0f0f0f0f0f0fL; 1831 i = i + (i >>> 8); 1832 i = i + (i >>> 16); 1833 i = i + (i >>> 32); 1834 return (int)i & 0x7f; 1835 } 1836 1837 /** 1838 * Returns the value obtained by rotating the two's complement binary 1839 * representation of the specified {@code long} value left by the 1840 * specified number of bits. (Bits shifted out of the left hand, or 1841 * high-order, side reenter on the right, or low-order.) 1842 * 1843 * <p>Note that left rotation with a negative distance is equivalent to 1844 * right rotation: {@code rotateLeft(val, -distance) == rotateRight(val, 1845 * distance)}. Note also that rotation by any multiple of 64 is a 1846 * no-op, so all but the last six bits of the rotation distance can be 1847 * ignored, even if the distance is negative: {@code rotateLeft(val, 1848 * distance) == rotateLeft(val, distance & 0x3F)}. 1849 * 1850 * @param i the value whose bits are to be rotated left 1851 * @param distance the number of bit positions to rotate left 1852 * @return the value obtained by rotating the two's complement binary 1853 * representation of the specified {@code long} value left by the 1854 * specified number of bits. 1855 * @since 1.5 1856 */ 1857 public static long rotateLeft(long i, int distance) { 1858 return (i << distance) | (i >>> -distance); 1859 } 1860 1861 /** 1862 * Returns the value obtained by rotating the two's complement binary 1863 * representation of the specified {@code long} value right by the 1864 * specified number of bits. (Bits shifted out of the right hand, or 1865 * low-order, side reenter on the left, or high-order.) 1866 * 1867 * <p>Note that right rotation with a negative distance is equivalent to 1868 * left rotation: {@code rotateRight(val, -distance) == rotateLeft(val, 1869 * distance)}. Note also that rotation by any multiple of 64 is a 1870 * no-op, so all but the last six bits of the rotation distance can be 1871 * ignored, even if the distance is negative: {@code rotateRight(val, 1872 * distance) == rotateRight(val, distance & 0x3F)}. 1873 * 1874 * @param i the value whose bits are to be rotated right 1875 * @param distance the number of bit positions to rotate right 1876 * @return the value obtained by rotating the two's complement binary 1877 * representation of the specified {@code long} value right by the 1878 * specified number of bits. 1879 * @since 1.5 1880 */ 1881 public static long rotateRight(long i, int distance) { 1882 return (i >>> distance) | (i << -distance); 1883 } 1884 1885 /** 1886 * Returns the value obtained by reversing the order of the bits in the 1887 * two's complement binary representation of the specified {@code long} 1888 * value. 1889 * 1890 * @param i the value to be reversed 1891 * @return the value obtained by reversing order of the bits in the 1892 * specified {@code long} value. 1893 * @since 1.5 1894 */ 1895 public static long reverse(long i) { 1896 // HD, Figure 7-1 1897 i = (i & 0x5555555555555555L) << 1 | (i >>> 1) & 0x5555555555555555L; 1898 i = (i & 0x3333333333333333L) << 2 | (i >>> 2) & 0x3333333333333333L; 1899 i = (i & 0x0f0f0f0f0f0f0f0fL) << 4 | (i >>> 4) & 0x0f0f0f0f0f0f0f0fL; 1900 1901 return reverseBytes(i); 1902 } 1903 1904 /** 1905 * Returns the signum function of the specified {@code long} value. (The 1906 * return value is -1 if the specified value is negative; 0 if the 1907 * specified value is zero; and 1 if the specified value is positive.) 1908 * 1909 * @param i the value whose signum is to be computed 1910 * @return the signum function of the specified {@code long} value. 1911 * @since 1.5 1912 */ 1913 public static int signum(long i) { 1914 // HD, Section 2-7 1915 return (int) ((i >> 63) | (-i >>> 63)); 1916 } 1917 1918 /** 1919 * Returns the value obtained by reversing the order of the bytes in the 1920 * two's complement representation of the specified {@code long} value. 1921 * 1922 * @param i the value whose bytes are to be reversed 1923 * @return the value obtained by reversing the bytes in the specified 1924 * {@code long} value. 1925 * @since 1.5 1926 */ 1927 @HotSpotIntrinsicCandidate 1928 public static long reverseBytes(long i) { 1929 i = (i & 0x00ff00ff00ff00ffL) << 8 | (i >>> 8) & 0x00ff00ff00ff00ffL; 1930 return (i << 48) | ((i & 0xffff0000L) << 16) | 1931 ((i >>> 16) & 0xffff0000L) | (i >>> 48); 1932 } 1933 1934 /** 1935 * Adds two {@code long} values together as per the + operator. 1936 * 1937 * @param a the first operand 1938 * @param b the second operand 1939 * @return the sum of {@code a} and {@code b} 1940 * @see java.util.function.BinaryOperator 1941 * @since 1.8 1942 */ 1943 public static long sum(long a, long b) { 1944 return a + b; 1945 } 1946 1947 /** 1948 * Returns the greater of two {@code long} values 1949 * as if by calling {@link Math#max(long, long) Math.max}. 1950 * 1951 * @param a the first operand 1952 * @param b the second operand 1953 * @return the greater of {@code a} and {@code b} 1954 * @see java.util.function.BinaryOperator 1955 * @since 1.8 1956 */ 1957 public static long max(long a, long b) { 1958 return Math.max(a, b); 1959 } 1960 1961 /** 1962 * Returns the smaller of two {@code long} values 1963 * as if by calling {@link Math#min(long, long) Math.min}. 1964 * 1965 * @param a the first operand 1966 * @param b the second operand 1967 * @return the smaller of {@code a} and {@code b} 1968 * @see java.util.function.BinaryOperator 1969 * @since 1.8 1970 */ 1971 public static long min(long a, long b) { 1972 return Math.min(a, b); 1973 } 1974 1975 /** use serialVersionUID from JDK 1.0.2 for interoperability */ 1976 @Native private static final long serialVersionUID = 4290774380558885855L; 1977 }