1 /*
   2  * Copyright (c) 1994, 2012, 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.util.Properties;
  30 
  31 /**
  32  * The {@code Integer} class wraps a value of the primitive type
  33  * {@code int} in an object. An object of type {@code Integer}
  34  * contains a single field whose type is {@code int}.
  35  *
  36  * <p>In addition, this class provides several methods for converting
  37  * an {@code int} to a {@code String} and a {@code String} to an
  38  * {@code int}, as well as other constants and methods useful when
  39  * dealing with an {@code int}.
  40  *
  41  * <p>Implementation note: The implementations of the "bit twiddling"
  42  * methods (such as {@link #highestOneBit(int) highestOneBit} and
  43  * {@link #numberOfTrailingZeros(int) numberOfTrailingZeros}) are
  44  * based on material from Henry S. Warren, Jr.'s <i>Hacker's
  45  * Delight</i>, (Addison Wesley, 2002).
  46  *
  47  * @author  Lee Boynton
  48  * @author  Arthur van Hoff
  49  * @author  Josh Bloch
  50  * @author  Joseph D. Darcy
  51  * @since JDK1.0
  52  */
  53 public final class Integer extends Number implements Comparable<Integer> {
  54     /**
  55      * A constant holding the minimum value an {@code int} can
  56      * have, -2<sup>31</sup>.
  57      */
  58     @Native public static final int   MIN_VALUE = 0x80000000;
  59 
  60     /**
  61      * A constant holding the maximum value an {@code int} can
  62      * have, 2<sup>31</sup>-1.
  63      */
  64     @Native public static final int   MAX_VALUE = 0x7fffffff;
  65 
  66     /**
  67      * The {@code Class} instance representing the primitive type
  68      * {@code int}.
  69      *
  70      * @since   JDK1.1
  71      */
  72     @SuppressWarnings("unchecked")
  73     public static final Class<Integer>  TYPE = (Class<Integer>) Class.getPrimitiveClass("int");
  74 
  75     /**
  76      * All possible chars for representing a number as a String
  77      */
  78     final static char[] digits = {
  79         '0' , '1' , '2' , '3' , '4' , '5' ,
  80         '6' , '7' , '8' , '9' , 'a' , 'b' ,
  81         'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
  82         'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
  83         'o' , 'p' , 'q' , 'r' , 's' , 't' ,
  84         'u' , 'v' , 'w' , 'x' , 'y' , 'z'
  85     };
  86 
  87     /**
  88      * Returns a string representation of the first argument in the
  89      * radix specified by the second argument.
  90      *
  91      * <p>If the radix is smaller than {@code Character.MIN_RADIX}
  92      * or larger than {@code Character.MAX_RADIX}, then the radix
  93      * {@code 10} is used instead.
  94      *
  95      * <p>If the first argument is negative, the first element of the
  96      * result is the ASCII minus character {@code '-'}
  97      * ({@code '\u005Cu002D'}). If the first argument is not
  98      * negative, no sign character appears in the result.
  99      *
 100      * <p>The remaining characters of the result represent the magnitude
 101      * of the first argument. If the magnitude is zero, it is
 102      * represented by a single zero character {@code '0'}
 103      * ({@code '\u005Cu0030'}); otherwise, the first character of
 104      * the representation of the magnitude will not be the zero
 105      * character.  The following ASCII characters are used as digits:
 106      *
 107      * <blockquote>
 108      *   {@code 0123456789abcdefghijklmnopqrstuvwxyz}
 109      * </blockquote>
 110      *
 111      * These are {@code '\u005Cu0030'} through
 112      * {@code '\u005Cu0039'} and {@code '\u005Cu0061'} through
 113      * {@code '\u005Cu007A'}. If {@code radix} is
 114      * <var>N</var>, then the first <var>N</var> of these characters
 115      * are used as radix-<var>N</var> digits in the order shown. Thus,
 116      * the digits for hexadecimal (radix 16) are
 117      * {@code 0123456789abcdef}. If uppercase letters are
 118      * desired, the {@link java.lang.String#toUpperCase()} method may
 119      * be called on the result:
 120      *
 121      * <blockquote>
 122      *  {@code Integer.toString(n, 16).toUpperCase()}
 123      * </blockquote>
 124      *
 125      * @param   i       an integer to be converted to a string.
 126      * @param   radix   the radix to use in the string representation.
 127      * @return  a string representation of the argument in the specified radix.
 128      * @see     java.lang.Character#MAX_RADIX
 129      * @see     java.lang.Character#MIN_RADIX
 130      */
 131     public static String toString(int i, int radix) {
 132         if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
 133             radix = 10;
 134 
 135         /* Use the faster version */
 136         if (radix == 10) {
 137             return toString(i);
 138         }
 139 
 140         char buf[] = new char[33];
 141         boolean negative = (i < 0);
 142         int charPos = 32;
 143 
 144         if (!negative) {
 145             i = -i;
 146         }
 147 
 148         while (i <= -radix) {
 149             buf[charPos--] = digits[-(i % radix)];
 150             i = i / radix;
 151         }
 152         buf[charPos] = digits[-i];
 153 
 154         if (negative) {
 155             buf[--charPos] = '-';
 156         }
 157 
 158         return new String(buf, charPos, (33 - charPos));
 159     }
 160 
 161     /**
 162      * Returns a string representation of the first argument as an
 163      * unsigned integer value in the radix specified by the second
 164      * argument.
 165      *
 166      * <p>If the radix is smaller than {@code Character.MIN_RADIX}
 167      * or larger than {@code Character.MAX_RADIX}, then the radix
 168      * {@code 10} is used instead.
 169      *
 170      * <p>Note that since the first argument is treated as an unsigned
 171      * value, no leading sign character is printed.
 172      *
 173      * <p>If the magnitude is zero, it is represented by a single zero
 174      * character {@code '0'} ({@code '\u005Cu0030'}); otherwise,
 175      * the first character of the representation of the magnitude will
 176      * not be the zero character.
 177      *
 178      * <p>The behavior of radixes and the characters used as digits
 179      * are the same as {@link #toString(int, int) toString}.
 180      *
 181      * @param   i       an integer to be converted to an unsigned string.
 182      * @param   radix   the radix to use in the string representation.
 183      * @return  an unsigned string representation of the argument in the specified radix.
 184      * @see     #toString(int, int)
 185      * @since 1.8
 186      */
 187     public static String toUnsignedString(int i, int radix) {
 188         return Long.toString(toUnsignedLong(i), radix);
 189     }
 190 
 191     /**
 192      * Returns a string representation of the integer argument as an
 193      * unsigned integer in base&nbsp;16.
 194      *
 195      * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
 196      * if the argument is negative; otherwise, it is equal to the
 197      * argument.  This value is converted to a string of ASCII digits
 198      * in hexadecimal (base&nbsp;16) with no extra leading
 199      * {@code 0}s.
 200      *
 201      * <p>The value of the argument can be recovered from the returned
 202      * string {@code s} by calling {@link
 203      * Integer#parseUnsignedInt(String, int)
 204      * Integer.parseUnsignedInt(s, 16)}.
 205      *
 206      * <p>If the unsigned magnitude is zero, it is represented by a
 207      * single zero character {@code '0'} ({@code '\u005Cu0030'});
 208      * otherwise, the first character of the representation of the
 209      * unsigned magnitude will not be the zero character. The
 210      * following characters are used as hexadecimal digits:
 211      *
 212      * <blockquote>
 213      *  {@code 0123456789abcdef}
 214      * </blockquote>
 215      *
 216      * These are the characters {@code '\u005Cu0030'} through
 217      * {@code '\u005Cu0039'} and {@code '\u005Cu0061'} through
 218      * {@code '\u005Cu0066'}. If uppercase letters are
 219      * desired, the {@link java.lang.String#toUpperCase()} method may
 220      * be called on the result:
 221      *
 222      * <blockquote>
 223      *  {@code Integer.toHexString(n).toUpperCase()}
 224      * </blockquote>
 225      *
 226      * @param   i   an integer to be converted to a string.
 227      * @return  the string representation of the unsigned integer value
 228      *          represented by the argument in hexadecimal (base&nbsp;16).
 229      * @see #parseUnsignedInt(String, int)
 230      * @see #toUnsignedString(int, int)
 231      * @since   JDK1.0.2
 232      */
 233     public static String toHexString(int i) {
 234         return toUnsignedString0(i, 4);
 235     }
 236 
 237     /**
 238      * Returns a string representation of the integer argument as an
 239      * unsigned integer in base&nbsp;8.
 240      *
 241      * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
 242      * if the argument is negative; otherwise, it is equal to the
 243      * argument.  This value is converted to a string of ASCII digits
 244      * in octal (base&nbsp;8) with no extra leading {@code 0}s.
 245      *
 246      * <p>The value of the argument can be recovered from the returned
 247      * string {@code s} by calling {@link
 248      * Integer#parseUnsignedInt(String, int)
 249      * Integer.parseUnsignedInt(s, 8)}.
 250      *
 251      * <p>If the unsigned magnitude is zero, it is represented by a
 252      * single zero character {@code '0'} ({@code '\u005Cu0030'});
 253      * otherwise, the first character of the representation of the
 254      * unsigned magnitude will not be the zero character. The
 255      * following characters are used as octal digits:
 256      *
 257      * <blockquote>
 258      * {@code 01234567}
 259      * </blockquote>
 260      *
 261      * These are the characters {@code '\u005Cu0030'} through
 262      * {@code '\u005Cu0037'}.
 263      *
 264      * @param   i   an integer to be converted to a string.
 265      * @return  the string representation of the unsigned integer value
 266      *          represented by the argument in octal (base&nbsp;8).
 267      * @see #parseUnsignedInt(String, int)
 268      * @see #toUnsignedString(int, int)
 269      * @since   JDK1.0.2
 270      */
 271     public static String toOctalString(int i) {
 272         return toUnsignedString0(i, 3);
 273     }
 274 
 275     /**
 276      * Returns a string representation of the integer argument as an
 277      * unsigned integer in base&nbsp;2.
 278      *
 279      * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
 280      * if the argument is negative; otherwise it is equal to the
 281      * argument.  This value is converted to a string of ASCII digits
 282      * in binary (base&nbsp;2) with no extra leading {@code 0}s.
 283      *
 284      * <p>The value of the argument can be recovered from the returned
 285      * string {@code s} by calling {@link
 286      * Integer#parseUnsignedInt(String, int)
 287      * Integer.parseUnsignedInt(s, 2)}.
 288      *
 289      * <p>If the unsigned magnitude is zero, it is represented by a
 290      * single zero character {@code '0'} ({@code '\u005Cu0030'});
 291      * otherwise, the first character of the representation of the
 292      * unsigned magnitude will not be the zero character. The
 293      * characters {@code '0'} ({@code '\u005Cu0030'}) and {@code
 294      * '1'} ({@code '\u005Cu0031'}) are used as binary digits.
 295      *
 296      * @param   i   an integer to be converted to a string.
 297      * @return  the string representation of the unsigned integer value
 298      *          represented by the argument in binary (base&nbsp;2).
 299      * @see #parseUnsignedInt(String, int)
 300      * @see #toUnsignedString(int, int)
 301      * @since   JDK1.0.2
 302      */
 303     public static String toBinaryString(int i) {
 304         return toUnsignedString0(i, 1);
 305     }
 306 
 307     /**
 308      * Convert the integer to an unsigned number.
 309      */
 310     private static String toUnsignedString0(int i, int shift) {
 311         char[] buf = new char[32];
 312         int charPos = 32;
 313         int radix = 1 << shift;
 314         int mask = radix - 1;
 315         do {
 316             buf[--charPos] = digits[i & mask];
 317             i >>>= shift;
 318         } while (i != 0);
 319 
 320         return new String(buf, charPos, (32 - charPos));
 321     }
 322 
 323 
 324     final static char [] DigitTens = {
 325         '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
 326         '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
 327         '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
 328         '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
 329         '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
 330         '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
 331         '6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
 332         '7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
 333         '8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
 334         '9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
 335         } ;
 336 
 337     final static char [] DigitOnes = {
 338         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 339         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 340         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 341         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 342         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 343         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 344         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 345         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 346         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 347         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 348         } ;
 349 
 350         // I use the "invariant division by multiplication" trick to
 351         // accelerate Integer.toString.  In particular we want to
 352         // avoid division by 10.
 353         //
 354         // The "trick" has roughly the same performance characteristics
 355         // as the "classic" Integer.toString code on a non-JIT VM.
 356         // The trick avoids .rem and .div calls but has a longer code
 357         // path and is thus dominated by dispatch overhead.  In the
 358         // JIT case the dispatch overhead doesn't exist and the
 359         // "trick" is considerably faster than the classic code.
 360         //
 361         // TODO-FIXME: convert (x * 52429) into the equiv shift-add
 362         // sequence.
 363         //
 364         // RE:  Division by Invariant Integers using Multiplication
 365         //      T Gralund, P Montgomery
 366         //      ACM PLDI 1994
 367         //
 368 
 369     /**
 370      * Returns a {@code String} object representing the
 371      * specified integer. The argument is converted to signed decimal
 372      * representation and returned as a string, exactly as if the
 373      * argument and radix 10 were given as arguments to the {@link
 374      * #toString(int, int)} method.
 375      *
 376      * @param   i   an integer to be converted.
 377      * @return  a string representation of the argument in base&nbsp;10.
 378      */
 379     public static String toString(int i) {
 380         if (i == Integer.MIN_VALUE)
 381             return "-2147483648";
 382         int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
 383         char[] buf = new char[size];
 384         getChars(i, size, buf);
 385         return new String(buf, true);
 386     }
 387 
 388     /**
 389      * Returns a string representation of the argument as an unsigned
 390      * decimal value.
 391      *
 392      * The argument is converted to unsigned decimal representation
 393      * and returned as a string exactly as if the argument and radix
 394      * 10 were given as arguments to the {@link #toUnsignedString(int,
 395      * int)} method.
 396      *
 397      * @param   i  an integer to be converted to an unsigned string.
 398      * @return  an unsigned string representation of the argument.
 399      * @see     #toUnsignedString(int, int)
 400      * @since 1.8
 401      */
 402     public static String toUnsignedString(int i) {
 403         return Long.toString(toUnsignedLong(i));
 404     }
 405 
 406     /**
 407      * Places characters representing the integer i into the
 408      * character array buf. The characters are placed into
 409      * the buffer backwards starting with the least significant
 410      * digit at the specified index (exclusive), and working
 411      * backwards from there.
 412      *
 413      * Will fail if i == Integer.MIN_VALUE
 414      */
 415     static void getChars(int i, int index, char[] buf) {
 416         int q, r;
 417         int charPos = index;
 418         char sign = 0;
 419 
 420         if (i < 0) {
 421             sign = '-';
 422             i = -i;
 423         }
 424 
 425         // Generate two digits per iteration
 426         while (i >= 65536) {
 427             q = i / 100;
 428         // really: r = i - (q * 100);
 429             r = i - ((q << 6) + (q << 5) + (q << 2));
 430             i = q;
 431             buf [--charPos] = DigitOnes[r];
 432             buf [--charPos] = DigitTens[r];
 433         }
 434 
 435         // Fall thru to fast mode for smaller numbers
 436         // assert(i <= 65536, i);
 437         for (;;) {
 438             q = (i * 52429) >>> (16+3);
 439             r = i - ((q << 3) + (q << 1));  // r = i-(q*10) ...
 440             buf [--charPos] = digits [r];
 441             i = q;
 442             if (i == 0) break;
 443         }
 444         if (sign != 0) {
 445             buf [--charPos] = sign;
 446         }
 447     }
 448 
 449     final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
 450                                       99999999, 999999999, Integer.MAX_VALUE };
 451 
 452     // Requires positive x
 453     static int stringSize(int x) {
 454         for (int i=0; ; i++)
 455             if (x <= sizeTable[i])
 456                 return i+1;
 457     }
 458 
 459     /**
 460      * Parses the string argument as a signed integer in the radix
 461      * specified by the second argument. The characters in the string
 462      * must all be digits of the specified radix (as determined by
 463      * whether {@link java.lang.Character#digit(char, int)} returns a
 464      * nonnegative value), except that the first character may be an
 465      * ASCII minus sign {@code '-'} ({@code '\u005Cu002D'}) to
 466      * indicate a negative value or an ASCII plus sign {@code '+'}
 467      * ({@code '\u005Cu002B'}) to indicate a positive value. The
 468      * resulting integer value is returned.
 469      *
 470      * <p>An exception of type {@code NumberFormatException} is
 471      * thrown if any of the following situations occurs:
 472      * <ul>
 473      * <li>The first argument is {@code null} or is a string of
 474      * length zero.
 475      *
 476      * <li>The radix is either smaller than
 477      * {@link java.lang.Character#MIN_RADIX} or
 478      * larger than {@link java.lang.Character#MAX_RADIX}.
 479      *
 480      * <li>Any character of the string is not a digit of the specified
 481      * radix, except that the first character may be a minus sign
 482      * {@code '-'} ({@code '\u005Cu002D'}) or plus sign
 483      * {@code '+'} ({@code '\u005Cu002B'}) provided that the
 484      * string is longer than length 1.
 485      *
 486      * <li>The value represented by the string is not a value of type
 487      * {@code int}.
 488      * </ul>
 489      *
 490      * <p>Examples:
 491      * <blockquote><pre>
 492      * parseInt("0", 10) returns 0
 493      * parseInt("473", 10) returns 473
 494      * parseInt("+42", 10) returns 42
 495      * parseInt("-0", 10) returns 0
 496      * parseInt("-FF", 16) returns -255
 497      * parseInt("1100110", 2) returns 102
 498      * parseInt("2147483647", 10) returns 2147483647
 499      * parseInt("-2147483648", 10) returns -2147483648
 500      * parseInt("2147483648", 10) throws a NumberFormatException
 501      * parseInt("99", 8) throws a NumberFormatException
 502      * parseInt("Kona", 10) throws a NumberFormatException
 503      * parseInt("Kona", 27) returns 411787
 504      * </pre></blockquote>
 505      *
 506      * @param      s   the {@code String} containing the integer
 507      *                  representation to be parsed
 508      * @param      radix   the radix to be used while parsing {@code s}.
 509      * @return     the integer represented by the string argument in the
 510      *             specified radix.
 511      * @exception  NumberFormatException if the {@code String}
 512      *             does not contain a parsable {@code int}.
 513      */
 514     public static int parseInt(String s, int radix)
 515                 throws NumberFormatException
 516     {
 517         /*
 518          * WARNING: This method may be invoked early during VM initialization
 519          * before IntegerCache is initialized. Care must be taken to not use
 520          * the valueOf method.
 521          */
 522 
 523         if (s == null) {
 524             throw new NumberFormatException("null");
 525         }
 526 
 527         if (radix < Character.MIN_RADIX) {
 528             throw new NumberFormatException("radix " + radix +
 529                                             " less than Character.MIN_RADIX");
 530         }
 531 
 532         if (radix > Character.MAX_RADIX) {
 533             throw new NumberFormatException("radix " + radix +
 534                                             " greater than Character.MAX_RADIX");
 535         }
 536 
 537         int result = 0;
 538         boolean negative = false;
 539         int i = 0, len = s.length();
 540         int limit = -Integer.MAX_VALUE;
 541         int multmin;
 542         int digit;
 543 
 544         if (len > 0) {
 545             char firstChar = s.charAt(0);
 546             if (firstChar < '0') { // Possible leading "+" or "-"
 547                 if (firstChar == '-') {
 548                     negative = true;
 549                     limit = Integer.MIN_VALUE;
 550                 } else if (firstChar != '+')
 551                     throw NumberFormatException.forInputString(s);
 552 
 553                 if (len == 1) // Cannot have lone "+" or "-"
 554                     throw NumberFormatException.forInputString(s);
 555                 i++;
 556             }
 557             multmin = limit / radix;
 558             while (i < len) {
 559                 // Accumulating negatively avoids surprises near MAX_VALUE
 560                 digit = Character.digit(s.charAt(i++),radix);
 561                 if (digit < 0) {
 562                     throw NumberFormatException.forInputString(s);
 563                 }
 564                 if (result < multmin) {
 565                     throw NumberFormatException.forInputString(s);
 566                 }
 567                 result *= radix;
 568                 if (result < limit + digit) {
 569                     throw NumberFormatException.forInputString(s);
 570                 }
 571                 result -= digit;
 572             }
 573         } else {
 574             throw NumberFormatException.forInputString(s);
 575         }
 576         return negative ? result : -result;
 577     }
 578 
 579     /**
 580      * Parses the string argument as a signed decimal integer. The
 581      * characters in the string must all be decimal digits, except
 582      * that the first character may be an ASCII minus sign {@code '-'}
 583      * ({@code '\u005Cu002D'}) to indicate a negative value or an
 584      * ASCII plus sign {@code '+'} ({@code '\u005Cu002B'}) to
 585      * indicate a positive value. The resulting integer value is
 586      * returned, exactly as if the argument and the radix 10 were
 587      * given as arguments to the {@link #parseInt(java.lang.String,
 588      * int)} method.
 589      *
 590      * @param s    a {@code String} containing the {@code int}
 591      *             representation to be parsed
 592      * @return     the integer value represented by the argument in decimal.
 593      * @exception  NumberFormatException  if the string does not contain a
 594      *               parsable integer.
 595      */
 596     public static int parseInt(String s) throws NumberFormatException {
 597         return parseInt(s,10);
 598     }
 599 
 600     /**
 601      * Parses the string argument as an unsigned integer in the radix
 602      * specified by the second argument.  An unsigned integer maps the
 603      * values usually associated with negative numbers to positive
 604      * numbers larger than {@code MAX_VALUE}.
 605      *
 606      * The characters in the string must all be digits of the
 607      * specified radix (as determined by whether {@link
 608      * java.lang.Character#digit(char, int)} returns a nonnegative
 609      * value), except that the first character may be an ASCII plus
 610      * sign {@code '+'} ({@code '\u005Cu002B'}). The resulting
 611      * integer value is returned.
 612      *
 613      * <p>An exception of type {@code NumberFormatException} is
 614      * thrown if any of the following situations occurs:
 615      * <ul>
 616      * <li>The first argument is {@code null} or is a string of
 617      * length zero.
 618      *
 619      * <li>The radix is either smaller than
 620      * {@link java.lang.Character#MIN_RADIX} or
 621      * larger than {@link java.lang.Character#MAX_RADIX}.
 622      *
 623      * <li>Any character of the string is not a digit of the specified
 624      * radix, except that the first character may be a plus sign
 625      * {@code '+'} ({@code '\u005Cu002B'}) provided that the
 626      * string is longer than length 1.
 627      *
 628      * <li>The value represented by the string is larger than the
 629      * largest unsigned {@code int}, 2<sup>32</sup>-1.
 630      *
 631      * </ul>
 632      *
 633      *
 634      * @param      s   the {@code String} containing the unsigned integer
 635      *                  representation to be parsed
 636      * @param      radix   the radix to be used while parsing {@code s}.
 637      * @return     the integer represented by the string argument in the
 638      *             specified radix.
 639      * @throws     NumberFormatException if the {@code String}
 640      *             does not contain a parsable {@code int}.
 641      * @since 1.8
 642      */
 643     public static int parseUnsignedInt(String s, int radix)
 644                 throws NumberFormatException {
 645         if (s == null)  {
 646             throw new NumberFormatException("null");
 647         }
 648 
 649         int len = s.length();
 650         if (len > 0) {
 651             char firstChar = s.charAt(0);
 652             if (firstChar == '-') {
 653                 throw new
 654                     NumberFormatException(String.format("Illegal leading minus sign " +
 655                                                        "on unsigned string %s.", s));
 656             } else {
 657                 if (len <= 5 || // Integer.MAX_VALUE in Character.MAX_RADIX is 6 digits
 658                     (radix == 10 && len <= 9) ) { // Integer.MAX_VALUE in base 10 is 10 digits
 659                     return parseInt(s, radix);
 660                 } else {
 661                     long ell = Long.parseLong(s, radix);
 662                     if ((ell & 0xffff_ffff_0000_0000L) == 0) {
 663                         return (int) ell;
 664                     } else {
 665                         throw new
 666                             NumberFormatException(String.format("String value %s exceeds " +
 667                                                                 "range of unsigned int.", s));
 668                     }
 669                 }
 670             }
 671         } else {
 672             throw NumberFormatException.forInputString(s);
 673         }
 674     }
 675 
 676     /**
 677      * Parses the string argument as an unsigned decimal integer. The
 678      * characters in the string must all be decimal digits, except
 679      * that the first character may be an an ASCII plus sign {@code
 680      * '+'} ({@code '\u005Cu002B'}). The resulting integer value
 681      * is returned, exactly as if the argument and the radix 10 were
 682      * given as arguments to the {@link
 683      * #parseUnsignedInt(java.lang.String, int)} method.
 684      *
 685      * @param s   a {@code String} containing the unsigned {@code int}
 686      *            representation to be parsed
 687      * @return    the unsigned integer value represented by the argument in decimal.
 688      * @throws    NumberFormatException  if the string does not contain a
 689      *            parsable unsigned integer.
 690      * @since 1.8
 691      */
 692     public static int parseUnsignedInt(String s) throws NumberFormatException {
 693         return parseUnsignedInt(s, 10);
 694     }
 695 
 696     /**
 697      * Returns an {@code Integer} object holding the value
 698      * extracted from the specified {@code String} when parsed
 699      * with the radix given by the second argument. The first argument
 700      * is interpreted as representing a signed integer in the radix
 701      * specified by the second argument, exactly as if the arguments
 702      * were given to the {@link #parseInt(java.lang.String, int)}
 703      * method. The result is an {@code Integer} object that
 704      * represents the integer value specified by the string.
 705      *
 706      * <p>In other words, this method returns an {@code Integer}
 707      * object equal to the value of:
 708      *
 709      * <blockquote>
 710      *  {@code new Integer(Integer.parseInt(s, radix))}
 711      * </blockquote>
 712      *
 713      * @param      s   the string to be parsed.
 714      * @param      radix the radix to be used in interpreting {@code s}
 715      * @return     an {@code Integer} object holding the value
 716      *             represented by the string argument in the specified
 717      *             radix.
 718      * @exception NumberFormatException if the {@code String}
 719      *            does not contain a parsable {@code int}.
 720      */
 721     public static Integer valueOf(String s, int radix) throws NumberFormatException {
 722         return Integer.valueOf(parseInt(s,radix));
 723     }
 724 
 725     /**
 726      * Returns an {@code Integer} object holding the
 727      * value of the specified {@code String}. The argument is
 728      * interpreted as representing a signed decimal integer, exactly
 729      * as if the argument were given to the {@link
 730      * #parseInt(java.lang.String)} method. The result is an
 731      * {@code Integer} object that represents the integer value
 732      * specified by the string.
 733      *
 734      * <p>In other words, this method returns an {@code Integer}
 735      * object equal to the value of:
 736      *
 737      * <blockquote>
 738      *  {@code new Integer(Integer.parseInt(s))}
 739      * </blockquote>
 740      *
 741      * @param      s   the string to be parsed.
 742      * @return     an {@code Integer} object holding the value
 743      *             represented by the string argument.
 744      * @exception  NumberFormatException  if the string cannot be parsed
 745      *             as an integer.
 746      */
 747     public static Integer valueOf(String s) throws NumberFormatException {
 748         return Integer.valueOf(parseInt(s, 10));
 749     }
 750 
 751     /**
 752      * Cache to support the object identity semantics of autoboxing for values between
 753      * -128 and 127 (inclusive) as required by JLS.
 754      *
 755      * The cache is initialized on first usage.  The size of the cache
 756      * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
 757      * During VM initialization, java.lang.Integer.IntegerCache.high property
 758      * may be set and saved in the private system properties in the
 759      * sun.misc.VM class.
 760      */
 761 
 762     private static class IntegerCache {
 763         static final int low = -128;
 764         static final int high;
 765         static final Integer cache[];
 766 
 767         static {
 768             // high value may be configured by property
 769             int h = 127;
 770             String integerCacheHighPropValue =
 771                 sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
 772             if (integerCacheHighPropValue != null) {
 773                 try {
 774                     int i = parseInt(integerCacheHighPropValue);
 775                     i = Math.max(i, 127);
 776                     // Maximum array size is Integer.MAX_VALUE
 777                     h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
 778                 } catch( NumberFormatException nfe) {
 779                     // If the property cannot be parsed into an int, ignore it.
 780                 }
 781             }
 782             high = h;
 783 
 784             cache = new Integer[(high - low) + 1];
 785             int j = low;
 786             for(int k = 0; k < cache.length; k++)
 787                 cache[k] = new Integer(j++);
 788 
 789             // range [-128, 127] must be interned (JLS7 5.1.7)
 790             assert IntegerCache.high >= 127;
 791         }
 792 
 793         private IntegerCache() {}
 794     }
 795 
 796     /**
 797      * Returns an {@code Integer} instance representing the specified
 798      * {@code int} value.  If a new {@code Integer} instance is not
 799      * required, this method should generally be used in preference to
 800      * the constructor {@link #Integer(int)}, as this method is likely
 801      * to yield significantly better space and time performance by
 802      * caching frequently requested values.
 803      *
 804      * This method will always cache values in the range -128 to 127,
 805      * inclusive, and may cache other values outside of this range.
 806      *
 807      * @param  i an {@code int} value.
 808      * @return an {@code Integer} instance representing {@code i}.
 809      * @since  1.5
 810      */
 811     public static Integer valueOf(int i) {
 812         if (i >= IntegerCache.low && i <= IntegerCache.high)
 813             return IntegerCache.cache[i + (-IntegerCache.low)];
 814         return new Integer(i);
 815     }
 816 
 817     /**
 818      * The value of the {@code Integer}.
 819      *
 820      * @serial
 821      */
 822     private final int value;
 823 
 824     /**
 825      * Constructs a newly allocated {@code Integer} object that
 826      * represents the specified {@code int} value.
 827      *
 828      * @param   value   the value to be represented by the
 829      *                  {@code Integer} object.
 830      */
 831     public Integer(int value) {
 832         this.value = value;
 833     }
 834 
 835     /**
 836      * Constructs a newly allocated {@code Integer} object that
 837      * represents the {@code int} value indicated by the
 838      * {@code String} parameter. The string is converted to an
 839      * {@code int} value in exactly the manner used by the
 840      * {@code parseInt} method for radix 10.
 841      *
 842      * @param      s   the {@code String} to be converted to an
 843      *                 {@code Integer}.
 844      * @exception  NumberFormatException  if the {@code String} does not
 845      *               contain a parsable integer.
 846      * @see        java.lang.Integer#parseInt(java.lang.String, int)
 847      */
 848     public Integer(String s) throws NumberFormatException {
 849         this.value = parseInt(s, 10);
 850     }
 851 
 852     /**
 853      * Returns the value of this {@code Integer} as a {@code byte}
 854      * after a narrowing primitive conversion.
 855      * @jls 5.1.3 Narrowing Primitive Conversions
 856      */
 857     public byte byteValue() {
 858         return (byte)value;
 859     }
 860 
 861     /**
 862      * Returns the value of this {@code Integer} as a {@code short}
 863      * after a narrowing primitive conversion.
 864      * @jls 5.1.3 Narrowing Primitive Conversions
 865      */
 866     public short shortValue() {
 867         return (short)value;
 868     }
 869 
 870     /**
 871      * Returns the value of this {@code Integer} as an
 872      * {@code int}.
 873      */
 874     public int intValue() {
 875         return value;
 876     }
 877 
 878     /**
 879      * Returns the value of this {@code Integer} as a {@code long}
 880      * after a widening primitive conversion.
 881      * @jls 5.1.2 Widening Primitive Conversions
 882      */
 883     public long longValue() {
 884         return (long)value;
 885     }
 886 
 887     /**
 888      * Returns the value of this {@code Integer} as a {@code float}
 889      * after a widening primitive conversion.
 890      * @jls 5.1.2 Widening Primitive Conversions
 891      */
 892     public float floatValue() {
 893         return (float)value;
 894     }
 895 
 896     /**
 897      * Returns the value of this {@code Integer} as a {@code double}
 898      * after a widening primitive conversion.
 899      * @jls 5.1.2 Widening Primitive Conversions
 900      */
 901     public double doubleValue() {
 902         return (double)value;
 903     }
 904 
 905     /**
 906      * Returns a {@code String} object representing this
 907      * {@code Integer}'s value. The value is converted to signed
 908      * decimal representation and returned as a string, exactly as if
 909      * the integer value were given as an argument to the {@link
 910      * java.lang.Integer#toString(int)} method.
 911      *
 912      * @return  a string representation of the value of this object in
 913      *          base&nbsp;10.
 914      */
 915     public String toString() {
 916         return toString(value);
 917     }
 918 
 919     /**
 920      * Returns a hash code for this {@code Integer}.
 921      *
 922      * @return  a hash code value for this object, equal to the
 923      *          primitive {@code int} value represented by this
 924      *          {@code Integer} object.
 925      */
 926     @Override
 927     public int hashCode() {
 928         return Integer.hashCode(value);
 929     }
 930 
 931     /**
 932      * Returns a hash code for a {@code int} value; compatible with
 933      * {@code Integer.hashCode()}.
 934      *
 935      * @since 1.8
 936      *
 937      * @return a hash code value for a {@code int} value.
 938      */
 939     public static int hashCode(int value) {
 940         return value;
 941     }
 942 
 943     /**
 944      * Compares this object to the specified object.  The result is
 945      * {@code true} if and only if the argument is not
 946      * {@code null} and is an {@code Integer} object that
 947      * contains the same {@code int} value as this object.
 948      *
 949      * @param   obj   the object to compare with.
 950      * @return  {@code true} if the objects are the same;
 951      *          {@code false} otherwise.
 952      */
 953     public boolean equals(Object obj) {
 954         if (obj instanceof Integer) {
 955             return value == ((Integer)obj).intValue();
 956         }
 957         return false;
 958     }
 959 
 960     /**
 961      * Determines the integer value of the system property with the
 962      * specified name.
 963      *
 964      * <p>The first argument is treated as the name of a system
 965      * property.  System properties are accessible through the {@link
 966      * java.lang.System#getProperty(java.lang.String)} method. The
 967      * string value of this property is then interpreted as an integer
 968      * value using the grammar supported by {@link Integer#decode decode} and
 969      * an {@code Integer} object representing this value is returned.
 970      *
 971      * <p>If there is no property with the specified name, if the
 972      * specified name is empty or {@code null}, or if the property
 973      * does not have the correct numeric format, then {@code null} is
 974      * returned.
 975      *
 976      * <p>In other words, this method returns an {@code Integer}
 977      * object equal to the value of:
 978      *
 979      * <blockquote>
 980      *  {@code getInteger(nm, null)}
 981      * </blockquote>
 982      *
 983      * @param   nm   property name.
 984      * @return  the {@code Integer} value of the property.
 985      * @throws  SecurityException for the same reasons as
 986      *          {@link System#getProperty(String) System.getProperty}
 987      * @see     java.lang.System#getProperty(java.lang.String)
 988      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
 989      */
 990     public static Integer getInteger(String nm) {
 991         return getInteger(nm, null);
 992     }
 993 
 994     /**
 995      * Determines the integer value of the system property with the
 996      * specified name.
 997      *
 998      * <p>The first argument is treated as the name of a system
 999      * property.  System properties are accessible through the {@link
1000      * java.lang.System#getProperty(java.lang.String)} method. The
1001      * string value of this property is then interpreted as an integer
1002      * value using the grammar supported by {@link Integer#decode decode} and
1003      * an {@code Integer} object representing this value is returned.
1004      *
1005      * <p>The second argument is the default value. An {@code Integer} object
1006      * that represents the value of the second argument is returned if there
1007      * is no property of the specified name, if the property does not have
1008      * the correct numeric format, or if the specified name is empty or
1009      * {@code null}.
1010      *
1011      * <p>In other words, this method returns an {@code Integer} object
1012      * equal to the value of:
1013      *
1014      * <blockquote>
1015      *  {@code getInteger(nm, new Integer(val))}
1016      * </blockquote>
1017      *
1018      * but in practice it may be implemented in a manner such as:
1019      *
1020      * <blockquote><pre>
1021      * Integer result = getInteger(nm, null);
1022      * return (result == null) ? new Integer(val) : result;
1023      * </pre></blockquote>
1024      *
1025      * to avoid the unnecessary allocation of an {@code Integer}
1026      * object when the default value is not needed.
1027      *
1028      * @param   nm   property name.
1029      * @param   val   default value.
1030      * @return  the {@code Integer} value of the property.
1031      * @throws  SecurityException for the same reasons as
1032      *          {@link System#getProperty(String) System.getProperty}
1033      * @see     java.lang.System#getProperty(java.lang.String)
1034      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
1035      */
1036     public static Integer getInteger(String nm, int val) {
1037         Integer result = getInteger(nm, null);
1038         return (result == null) ? Integer.valueOf(val) : result;
1039     }
1040 
1041     /**
1042      * Returns the integer value of the system property with the
1043      * specified name.  The first argument is treated as the name of a
1044      * system property.  System properties are accessible through the
1045      * {@link java.lang.System#getProperty(java.lang.String)} method.
1046      * The string value of this property is then interpreted as an
1047      * integer value, as per the {@link Integer#decode decode} method,
1048      * and an {@code Integer} object representing this value is
1049      * returned; in summary:
1050      *
1051      * <ul><li>If the property value begins with the two ASCII characters
1052      *         {@code 0x} or the ASCII character {@code #}, not
1053      *      followed by a minus sign, then the rest of it is parsed as a
1054      *      hexadecimal integer exactly as by the method
1055      *      {@link #valueOf(java.lang.String, int)} with radix 16.
1056      * <li>If the property value begins with the ASCII character
1057      *     {@code 0} followed by another character, it is parsed as an
1058      *     octal integer exactly as by the method
1059      *     {@link #valueOf(java.lang.String, int)} with radix 8.
1060      * <li>Otherwise, the property value is parsed as a decimal integer
1061      * exactly as by the method {@link #valueOf(java.lang.String, int)}
1062      * with radix 10.
1063      * </ul>
1064      *
1065      * <p>The second argument is the default value. The default value is
1066      * returned if there is no property of the specified name, if the
1067      * property does not have the correct numeric format, or if the
1068      * specified name is empty or {@code null}.
1069      *
1070      * @param   nm   property name.
1071      * @param   val   default value.
1072      * @return  the {@code Integer} value of the property.
1073      * @throws  SecurityException for the same reasons as
1074      *          {@link System#getProperty(String) System.getProperty}
1075      * @see     System#getProperty(java.lang.String)
1076      * @see     System#getProperty(java.lang.String, java.lang.String)
1077      */
1078     public static Integer getInteger(String nm, Integer val) {
1079         String v = null;
1080         try {
1081             v = System.getProperty(nm);
1082         } catch (IllegalArgumentException | NullPointerException e) {
1083         }
1084         if (v != null) {
1085             try {
1086                 return Integer.decode(v);
1087             } catch (NumberFormatException e) {
1088             }
1089         }
1090         return val;
1091     }
1092 
1093     /**
1094      * Decodes a {@code String} into an {@code Integer}.
1095      * Accepts decimal, hexadecimal, and octal numbers given
1096      * by the following grammar:
1097      *
1098      * <blockquote>
1099      * <dl>
1100      * <dt><i>DecodableString:</i>
1101      * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
1102      * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
1103      * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
1104      * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
1105      * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
1106      * <p>
1107      * <dt><i>Sign:</i>
1108      * <dd>{@code -}
1109      * <dd>{@code +}
1110      * </dl>
1111      * </blockquote>
1112      *
1113      * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>
1114      * are as defined in section 3.10.1 of
1115      * <cite>The Java&trade; Language Specification</cite>,
1116      * except that underscores are not accepted between digits.
1117      *
1118      * <p>The sequence of characters following an optional
1119      * sign and/or radix specifier ("{@code 0x}", "{@code 0X}",
1120      * "{@code #}", or leading zero) is parsed as by the {@code
1121      * Integer.parseInt} method with the indicated radix (10, 16, or
1122      * 8).  This sequence of characters must represent a positive
1123      * value or a {@link NumberFormatException} will be thrown.  The
1124      * result is negated if first character of the specified {@code
1125      * String} is the minus sign.  No whitespace characters are
1126      * permitted in the {@code String}.
1127      *
1128      * @param     nm the {@code String} to decode.
1129      * @return    an {@code Integer} object holding the {@code int}
1130      *             value represented by {@code nm}
1131      * @exception NumberFormatException  if the {@code String} does not
1132      *            contain a parsable integer.
1133      * @see java.lang.Integer#parseInt(java.lang.String, int)
1134      */
1135     public static Integer decode(String nm) throws NumberFormatException {
1136         int radix = 10;
1137         int index = 0;
1138         boolean negative = false;
1139         Integer result;
1140 
1141         if (nm.length() == 0)
1142             throw new NumberFormatException("Zero length string");
1143         char firstChar = nm.charAt(0);
1144         // Handle sign, if present
1145         if (firstChar == '-') {
1146             negative = true;
1147             index++;
1148         } else if (firstChar == '+')
1149             index++;
1150 
1151         // Handle radix specifier, if present
1152         if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
1153             index += 2;
1154             radix = 16;
1155         }
1156         else if (nm.startsWith("#", index)) {
1157             index ++;
1158             radix = 16;
1159         }
1160         else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
1161             index ++;
1162             radix = 8;
1163         }
1164 
1165         if (nm.startsWith("-", index) || nm.startsWith("+", index))
1166             throw new NumberFormatException("Sign character in wrong position");
1167 
1168         try {
1169             result = Integer.valueOf(nm.substring(index), radix);
1170             result = negative ? Integer.valueOf(-result.intValue()) : result;
1171         } catch (NumberFormatException e) {
1172             // If number is Integer.MIN_VALUE, we'll end up here. The next line
1173             // handles this case, and causes any genuine format error to be
1174             // rethrown.
1175             String constant = negative ? ("-" + nm.substring(index))
1176                                        : nm.substring(index);
1177             result = Integer.valueOf(constant, radix);
1178         }
1179         return result;
1180     }
1181 
1182     /**
1183      * Compares two {@code Integer} objects numerically.
1184      *
1185      * @param   anotherInteger   the {@code Integer} to be compared.
1186      * @return  the value {@code 0} if this {@code Integer} is
1187      *          equal to the argument {@code Integer}; a value less than
1188      *          {@code 0} if this {@code Integer} is numerically less
1189      *          than the argument {@code Integer}; and a value greater
1190      *          than {@code 0} if this {@code Integer} is numerically
1191      *           greater than the argument {@code Integer} (signed
1192      *           comparison).
1193      * @since   1.2
1194      */
1195     public int compareTo(Integer anotherInteger) {
1196         return compare(this.value, anotherInteger.value);
1197     }
1198 
1199     /**
1200      * Compares two {@code int} values numerically.
1201      * The value returned is identical to what would be returned by:
1202      * <pre>
1203      *    Integer.valueOf(x).compareTo(Integer.valueOf(y))
1204      * </pre>
1205      *
1206      * @param  x the first {@code int} to compare
1207      * @param  y the second {@code int} to compare
1208      * @return the value {@code 0} if {@code x == y};
1209      *         a value less than {@code 0} if {@code x < y}; and
1210      *         a value greater than {@code 0} if {@code x > y}
1211      * @since 1.7
1212      */
1213     public static int compare(int x, int y) {
1214         return (x < y) ? -1 : ((x == y) ? 0 : 1);
1215     }
1216 
1217     /**
1218      * Compares two {@code int} values numerically treating the values
1219      * as unsigned.
1220      *
1221      * @param  x the first {@code int} to compare
1222      * @param  y the second {@code int} to compare
1223      * @return the value {@code 0} if {@code x == y}; a value less
1224      *         than {@code 0} if {@code x < y} as unsigned values; and
1225      *         a value greater than {@code 0} if {@code x > y} as
1226      *         unsigned values
1227      * @since 1.8
1228      */
1229     public static int compareUnsigned(int x, int y) {
1230         return compare(x + MIN_VALUE, y + MIN_VALUE);
1231     }
1232 
1233     /**
1234      * Converts the argument to a {@code long} by an unsigned
1235      * conversion.  In an unsigned conversion to a {@code long}, the
1236      * high-order 32 bits of the {@code long} are zero and the
1237      * low-order 32 bits are equal to the bits of the integer
1238      * argument.
1239      *
1240      * Consequently, zero and positive {@code int} values are mapped
1241      * to a numerically equal {@code long} value and negative {@code
1242      * int} values are mapped to a {@code long} value equal to the
1243      * input plus 2<sup>32</sup>.
1244      *
1245      * @param  x the value to convert to an unsigned {@code long}
1246      * @return the argument converted to {@code long} by an unsigned
1247      *         conversion
1248      * @since 1.8
1249      */
1250     public static long toUnsignedLong(int x) {
1251         return ((long) x) & 0xffffffffL;
1252     }
1253 
1254     /**
1255      * Returns the unsigned quotient of dividing the first argument by
1256      * the second where each argument and the result is interpreted as
1257      * an unsigned value.
1258      *
1259      * <p>Note that in two's complement arithmetic, the three other
1260      * basic arithmetic operations of add, subtract, and multiply are
1261      * bit-wise identical if the two operands are regarded as both
1262      * being signed or both being unsigned.  Therefore separate {@code
1263      * addUnsigned}, etc. methods are not provided.
1264      *
1265      * @param dividend the value to be divided
1266      * @param divisor the value doing the dividing
1267      * @return the unsigned quotient of the first argument divided by
1268      * the second argument
1269      * @see #remainderUnsigned
1270      * @since 1.8
1271      */
1272     public static int divideUnsigned(int dividend, int divisor) {
1273         // In lieu of tricky code, for now just use long arithmetic.
1274         return (int)(toUnsignedLong(dividend) / toUnsignedLong(divisor));
1275     }
1276 
1277     /**
1278      * Returns the unsigned remainder from dividing the first argument
1279      * by the second where each argument and the result is interpreted
1280      * as an unsigned value.
1281      *
1282      * @param dividend the value to be divided
1283      * @param divisor the value doing the dividing
1284      * @return the unsigned remainder of the first argument divided by
1285      * the second argument
1286      * @see #divideUnsigned
1287      * @since 1.8
1288      */
1289     public static int remainderUnsigned(int dividend, int divisor) {
1290         // In lieu of tricky code, for now just use long arithmetic.
1291         return (int)(toUnsignedLong(dividend) % toUnsignedLong(divisor));
1292     }
1293 
1294 
1295     // Bit twiddling
1296 
1297     /**
1298      * The number of bits used to represent an {@code int} value in two's
1299      * complement binary form.
1300      *
1301      * @since 1.5
1302      */
1303     @Native public static final int SIZE = 32;
1304 
1305     /**
1306      * The number of bytes used to represent a {@code int} value in two's
1307      * complement binary form.
1308      *
1309      * @since 1.8
1310      */
1311     public static final int BYTES = SIZE / Byte.SIZE;
1312 
1313     /**
1314      * Returns an {@code int} value with at most a single one-bit, in the
1315      * position of the highest-order ("leftmost") one-bit in the specified
1316      * {@code int} value.  Returns zero if the specified value has no
1317      * one-bits in its two's complement binary representation, that is, if it
1318      * is equal to zero.
1319      *
1320      * @return an {@code int} value with a single one-bit, in the position
1321      *     of the highest-order one-bit in the specified value, or zero if
1322      *     the specified value is itself equal to zero.
1323      * @since 1.5
1324      */
1325     public static int highestOneBit(int i) {
1326         // HD, Figure 3-1
1327         i |= (i >>  1);
1328         i |= (i >>  2);
1329         i |= (i >>  4);
1330         i |= (i >>  8);
1331         i |= (i >> 16);
1332         return i - (i >>> 1);
1333     }
1334 
1335     /**
1336      * Returns an {@code int} value with at most a single one-bit, in the
1337      * position of the lowest-order ("rightmost") one-bit in the specified
1338      * {@code int} value.  Returns zero if the specified value has no
1339      * one-bits in its two's complement binary representation, that is, if it
1340      * is equal to zero.
1341      *
1342      * @return an {@code int} value with a single one-bit, in the position
1343      *     of the lowest-order one-bit in the specified value, or zero if
1344      *     the specified value is itself equal to zero.
1345      * @since 1.5
1346      */
1347     public static int lowestOneBit(int i) {
1348         // HD, Section 2-1
1349         return i & -i;
1350     }
1351 
1352     /**
1353      * Returns the number of zero bits preceding the highest-order
1354      * ("leftmost") one-bit in the two's complement binary representation
1355      * of the specified {@code int} value.  Returns 32 if the
1356      * specified value has no one-bits in its two's complement representation,
1357      * in other words if it is equal to zero.
1358      *
1359      * <p>Note that this method is closely related to the logarithm base 2.
1360      * For all positive {@code int} values x:
1361      * <ul>
1362      * <li>floor(log<sub>2</sub>(x)) = {@code 31 - numberOfLeadingZeros(x)}
1363      * <li>ceil(log<sub>2</sub>(x)) = {@code 32 - numberOfLeadingZeros(x - 1)}
1364      * </ul>
1365      *
1366      * @return the number of zero bits preceding the highest-order
1367      *     ("leftmost") one-bit in the two's complement binary representation
1368      *     of the specified {@code int} value, or 32 if the value
1369      *     is equal to zero.
1370      * @since 1.5
1371      */
1372     public static int numberOfLeadingZeros(int i) {
1373         // HD, Figure 5-6
1374         if (i == 0)
1375             return 32;
1376         int n = 1;
1377         if (i >>> 16 == 0) { n += 16; i <<= 16; }
1378         if (i >>> 24 == 0) { n +=  8; i <<=  8; }
1379         if (i >>> 28 == 0) { n +=  4; i <<=  4; }
1380         if (i >>> 30 == 0) { n +=  2; i <<=  2; }
1381         n -= i >>> 31;
1382         return n;
1383     }
1384 
1385     /**
1386      * Returns the number of zero bits following the lowest-order ("rightmost")
1387      * one-bit in the two's complement binary representation of the specified
1388      * {@code int} value.  Returns 32 if the specified value has no
1389      * one-bits in its two's complement representation, in other words if it is
1390      * equal to zero.
1391      *
1392      * @return the number of zero bits following the lowest-order ("rightmost")
1393      *     one-bit in the two's complement binary representation of the
1394      *     specified {@code int} value, or 32 if the value is equal
1395      *     to zero.
1396      * @since 1.5
1397      */
1398     public static int numberOfTrailingZeros(int i) {
1399         // HD, Figure 5-14
1400         int y;
1401         if (i == 0) return 32;
1402         int n = 31;
1403         y = i <<16; if (y != 0) { n = n -16; i = y; }
1404         y = i << 8; if (y != 0) { n = n - 8; i = y; }
1405         y = i << 4; if (y != 0) { n = n - 4; i = y; }
1406         y = i << 2; if (y != 0) { n = n - 2; i = y; }
1407         return n - ((i << 1) >>> 31);
1408     }
1409 
1410     /**
1411      * Returns the number of one-bits in the two's complement binary
1412      * representation of the specified {@code int} value.  This function is
1413      * sometimes referred to as the <i>population count</i>.
1414      *
1415      * @return the number of one-bits in the two's complement binary
1416      *     representation of the specified {@code int} value.
1417      * @since 1.5
1418      */
1419     public static int bitCount(int i) {
1420         // HD, Figure 5-2
1421         i = i - ((i >>> 1) & 0x55555555);
1422         i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
1423         i = (i + (i >>> 4)) & 0x0f0f0f0f;
1424         i = i + (i >>> 8);
1425         i = i + (i >>> 16);
1426         return i & 0x3f;
1427     }
1428 
1429     /**
1430      * Returns the value obtained by rotating the two's complement binary
1431      * representation of the specified {@code int} value left by the
1432      * specified number of bits.  (Bits shifted out of the left hand, or
1433      * high-order, side reenter on the right, or low-order.)
1434      *
1435      * <p>Note that left rotation with a negative distance is equivalent to
1436      * right rotation: {@code rotateLeft(val, -distance) == rotateRight(val,
1437      * distance)}.  Note also that rotation by any multiple of 32 is a
1438      * no-op, so all but the last five bits of the rotation distance can be
1439      * ignored, even if the distance is negative: {@code rotateLeft(val,
1440      * distance) == rotateLeft(val, distance & 0x1F)}.
1441      *
1442      * @return the value obtained by rotating the two's complement binary
1443      *     representation of the specified {@code int} value left by the
1444      *     specified number of bits.
1445      * @since 1.5
1446      */
1447     public static int rotateLeft(int i, int distance) {
1448         return (i << distance) | (i >>> -distance);
1449     }
1450 
1451     /**
1452      * Returns the value obtained by rotating the two's complement binary
1453      * representation of the specified {@code int} value right by the
1454      * specified number of bits.  (Bits shifted out of the right hand, or
1455      * low-order, side reenter on the left, or high-order.)
1456      *
1457      * <p>Note that right rotation with a negative distance is equivalent to
1458      * left rotation: {@code rotateRight(val, -distance) == rotateLeft(val,
1459      * distance)}.  Note also that rotation by any multiple of 32 is a
1460      * no-op, so all but the last five bits of the rotation distance can be
1461      * ignored, even if the distance is negative: {@code rotateRight(val,
1462      * distance) == rotateRight(val, distance & 0x1F)}.
1463      *
1464      * @return the value obtained by rotating the two's complement binary
1465      *     representation of the specified {@code int} value right by the
1466      *     specified number of bits.
1467      * @since 1.5
1468      */
1469     public static int rotateRight(int i, int distance) {
1470         return (i >>> distance) | (i << -distance);
1471     }
1472 
1473     /**
1474      * Returns the value obtained by reversing the order of the bits in the
1475      * two's complement binary representation of the specified {@code int}
1476      * value.
1477      *
1478      * @return the value obtained by reversing order of the bits in the
1479      *     specified {@code int} value.
1480      * @since 1.5
1481      */
1482     public static int reverse(int i) {
1483         // HD, Figure 7-1
1484         i = (i & 0x55555555) << 1 | (i >>> 1) & 0x55555555;
1485         i = (i & 0x33333333) << 2 | (i >>> 2) & 0x33333333;
1486         i = (i & 0x0f0f0f0f) << 4 | (i >>> 4) & 0x0f0f0f0f;
1487         i = (i << 24) | ((i & 0xff00) << 8) |
1488             ((i >>> 8) & 0xff00) | (i >>> 24);
1489         return i;
1490     }
1491 
1492     /**
1493      * Returns the signum function of the specified {@code int} value.  (The
1494      * return value is -1 if the specified value is negative; 0 if the
1495      * specified value is zero; and 1 if the specified value is positive.)
1496      *
1497      * @return the signum function of the specified {@code int} value.
1498      * @since 1.5
1499      */
1500     public static int signum(int i) {
1501         // HD, Section 2-7
1502         return (i >> 31) | (-i >>> 31);
1503     }
1504 
1505     /**
1506      * Returns the value obtained by reversing the order of the bytes in the
1507      * two's complement representation of the specified {@code int} value.
1508      *
1509      * @return the value obtained by reversing the bytes in the specified
1510      *     {@code int} value.
1511      * @since 1.5
1512      */
1513     public static int reverseBytes(int i) {
1514         return ((i >>> 24)           ) |
1515                ((i >>   8) &   0xFF00) |
1516                ((i <<   8) & 0xFF0000) |
1517                ((i << 24));
1518     }
1519 
1520     /**
1521      * Adds two integers together as per the + operator.
1522      *
1523      * @param a the first operand
1524      * @param b the second operand
1525      * @return the sum of {@code a} and {@code b}
1526      * @see java.util.function.BinaryOperator
1527      * @since 1.8
1528      */
1529     public static int sum(int a, int b) {
1530         return a + b;
1531     }
1532 
1533     /**
1534      * Returns the greater of two {@code int} values
1535      * as if by calling {@link Math#max(int, int) Math.max}.
1536      *
1537      * @param a the first operand
1538      * @param b the second operand
1539      * @return the greater of {@code a} and {@code b}
1540      * @see java.util.function.BinaryOperator
1541      * @since 1.8
1542      */
1543     public static int max(int a, int b) {
1544         return Math.max(a, b);
1545     }
1546 
1547     /**
1548      * Returns the smaller of two {@code int} values
1549      * as if by calling {@link Math#min(int, int) Math.min}.
1550      *
1551      * @param a the first operand
1552      * @param b the second operand
1553      * @return the smaller of {@code a} and {@code b}
1554      * @see java.util.function.BinaryOperator
1555      * @since 1.8
1556      */
1557     public static int min(int a, int b) {
1558         return Math.min(a, b);
1559     }
1560 
1561     /** use serialVersionUID from JDK 1.0.2 for interoperability */
1562     @Native private static final long serialVersionUID = 1360826667806852920L;
1563 }