< prev index next >

src/java.base/share/classes/java/lang/Integer.java

Print this page


   1 /*
   2  * Copyright (c) 1994, 2016, 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


 158             }
 159             buf[charPos] = (byte)digits[-i];
 160 
 161             if (negative) {
 162                 buf[--charPos] = '-';
 163             }
 164 
 165             return StringLatin1.newString(buf, charPos, (33 - charPos));
 166         }
 167         return toStringUTF16(i, radix);
 168     }
 169 
 170     private static String toStringUTF16(int i, int radix) {
 171         byte[] buf = new byte[33 * 2];
 172         boolean negative = (i < 0);
 173         int charPos = 32;
 174         if (!negative) {
 175             i = -i;
 176         }
 177         while (i <= -radix) {
 178             StringUTF16.putChar(buf, charPos--, digits[-(i % radix)]);
 179             i = i / radix;
 180         }
 181         StringUTF16.putChar(buf, charPos, digits[-i]);
 182 
 183         if (negative) {
 184             StringUTF16.putChar(buf, --charPos, '-');
 185         }
 186         return StringUTF16.newString(buf, charPos, (33 - charPos));
 187     }
 188 
 189     /**
 190      * Returns a string representation of the first argument as an
 191      * unsigned integer value in the radix specified by the second
 192      * argument.
 193      *
 194      * <p>If the radix is smaller than {@code Character.MIN_RADIX}
 195      * or larger than {@code Character.MAX_RADIX}, then the radix
 196      * {@code 10} is used instead.
 197      *
 198      * <p>Note that since the first argument is treated as an unsigned
 199      * value, no leading sign character is printed.
 200      *
 201      * <p>If the magnitude is zero, it is represented by a single zero
 202      * character {@code '0'} ({@code '\u005Cu0030'}); otherwise,
 203      * the first character of the representation of the magnitude will
 204      * not be the zero character.


 369         int radix = 1 << shift;
 370         int mask = radix - 1;
 371         do {
 372             buf[--charPos] = Integer.digits[val & mask];
 373             val >>>= shift;
 374         } while (charPos > offset);
 375     }
 376 
 377     /** byte[]/LATIN1 version    */
 378     static void formatUnsignedInt(int val, int shift, byte[] buf, int offset, int len) {
 379         int charPos = offset + len;
 380         int radix = 1 << shift;
 381         int mask = radix - 1;
 382         do {
 383             buf[--charPos] = (byte)Integer.digits[val & mask];
 384             val >>>= shift;
 385         } while (charPos > offset);
 386     }
 387 
 388     /** byte[]/UTF16 version    */
 389     static void formatUnsignedIntUTF16(int val, int shift, byte[] buf, int offset, int len) {
 390         int charPos = offset + len;
 391         int radix = 1 << shift;
 392         int mask = radix - 1;
 393         do {
 394             StringUTF16.putChar(buf, --charPos, Integer.digits[val & mask]);
 395             val >>>= shift;
 396         } while (charPos > offset);
 397     }
 398 
 399     static final byte[] DigitTens = {
 400         '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
 401         '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
 402         '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
 403         '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
 404         '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
 405         '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
 406         '6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
 407         '7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
 408         '8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
 409         '9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
 410         } ;
 411 
 412     static final byte[] DigitOnes = {
 413         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 414         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',


 425 
 426     /**
 427      * Returns a {@code String} object representing the
 428      * specified integer. The argument is converted to signed decimal
 429      * representation and returned as a string, exactly as if the
 430      * argument and radix 10 were given as arguments to the {@link
 431      * #toString(int, int)} method.
 432      *
 433      * @param   i   an integer to be converted.
 434      * @return  a string representation of the argument in base&nbsp;10.
 435      */
 436     @HotSpotIntrinsicCandidate
 437     public static String toString(int i) {
 438         int size = stringSize(i);
 439         if (COMPACT_STRINGS) {
 440             byte[] buf = new byte[size];
 441             getChars(i, size, buf);
 442             return new String(buf, LATIN1);
 443         } else {
 444             byte[] buf = new byte[size * 2];
 445             getCharsUTF16(i, size, buf);
 446             return new String(buf, UTF16);
 447         }
 448     }
 449 
 450     /**
 451      * Returns a string representation of the argument as an unsigned
 452      * decimal value.
 453      *
 454      * The argument is converted to unsigned decimal representation
 455      * and returned as a string exactly as if the argument and radix
 456      * 10 were given as arguments to the {@link #toUnsignedString(int,
 457      * int)} method.
 458      *
 459      * @param   i  an integer to be converted to an unsigned string.
 460      * @return  an unsigned string representation of the argument.
 461      * @see     #toUnsignedString(int, int)
 462      * @since 1.8
 463      */
 464     public static String toUnsignedString(int i) {
 465         return Long.toString(toUnsignedLong(i));


 495         while (i <= -100) {
 496             q = i / 100;
 497             r = (q * 100) - i;
 498             i = q;
 499             buf[--charPos] = DigitOnes[r];
 500             buf[--charPos] = DigitTens[r];
 501         }
 502 
 503         // We know there are at most two digits left at this point.
 504         q = i / 10;
 505         r = (q * 10) - i;
 506         buf[--charPos] = (byte)('0' + r);
 507 
 508         // Whatever left is the remaining digit.
 509         if (q < 0) {
 510             buf[--charPos] = (byte)('0' - q);
 511         }
 512 
 513         if (negative) {
 514             buf[--charPos] = (byte)'-';
 515         }
 516         return charPos;
 517     }
 518 
 519     /**
 520      * This is a variant of {@link #getChars(int, int, byte[])}, but for
 521      * UTF-16 coder.
 522      *
 523      * @param i     value to convert
 524      * @param index next index, after the least significant digit
 525      * @param buf   target buffer, UTF16-coded.
 526      * @return index of the most significant digit or minus sign, if present
 527      */
 528     static int getCharsUTF16(int i, int index, byte[] buf) {
 529         int q, r;
 530         int charPos = index;
 531 
 532         boolean negative = (i < 0);
 533         if (!negative) {
 534             i = -i;
 535         }
 536 
 537         // Get 2 digits/iteration using ints
 538         while (i <= -100) {
 539             q = i / 100;
 540             r = (q * 100) - i;
 541             i = q;
 542             StringUTF16.putChar(buf, --charPos, DigitOnes[r]);
 543             StringUTF16.putChar(buf, --charPos, DigitTens[r]);
 544         }
 545 
 546         // We know there are at most two digits left at this point.
 547         q = i / 10;
 548         r = (q * 10) - i;
 549         StringUTF16.putChar(buf, --charPos, '0' + r);
 550 
 551         // Whatever left is the remaining digit.
 552         if (q < 0) {
 553             StringUTF16.putChar(buf, --charPos, '0' - q);
 554         }
 555 
 556         if (negative) {
 557             StringUTF16.putChar(buf, --charPos, '-');
 558         }
 559         return charPos;
 560     }
 561 
 562     // Left here for compatibility reasons, see JDK-8143900.
 563     static final int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
 564                                       99999999, 999999999, Integer.MAX_VALUE };
 565 
 566     /**
 567      * Returns the string representation size for a given int value.
 568      *
 569      * @param x int value
 570      * @return string size
 571      *
 572      * @implNote There are other ways to compute this: e.g. binary search,
 573      * but values are biased heavily towards zero, and therefore linear search
 574      * wins. The iteration results are also routinely inlined in the generated
 575      * code after loop unrolling.
 576      */
 577     static int stringSize(int x) {


   1 /*
   2  * Copyright (c) 1994, 2017, 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


 158             }
 159             buf[charPos] = (byte)digits[-i];
 160 
 161             if (negative) {
 162                 buf[--charPos] = '-';
 163             }
 164 
 165             return StringLatin1.newString(buf, charPos, (33 - charPos));
 166         }
 167         return toStringUTF16(i, radix);
 168     }
 169 
 170     private static String toStringUTF16(int i, int radix) {
 171         byte[] buf = new byte[33 * 2];
 172         boolean negative = (i < 0);
 173         int charPos = 32;
 174         if (!negative) {
 175             i = -i;
 176         }
 177         while (i <= -radix) {
 178             StringUTF16.Trusted.putChar(buf, charPos--, digits[-(i % radix)]);
 179             i = i / radix;
 180         }
 181         StringUTF16.Trusted.putChar(buf, charPos, digits[-i]);
 182 
 183         if (negative) {
 184             StringUTF16.Trusted.putChar(buf, --charPos, '-');
 185         }
 186         return StringUTF16.newString(buf, charPos, (33 - charPos));
 187     }
 188 
 189     /**
 190      * Returns a string representation of the first argument as an
 191      * unsigned integer value in the radix specified by the second
 192      * argument.
 193      *
 194      * <p>If the radix is smaller than {@code Character.MIN_RADIX}
 195      * or larger than {@code Character.MAX_RADIX}, then the radix
 196      * {@code 10} is used instead.
 197      *
 198      * <p>Note that since the first argument is treated as an unsigned
 199      * value, no leading sign character is printed.
 200      *
 201      * <p>If the magnitude is zero, it is represented by a single zero
 202      * character {@code '0'} ({@code '\u005Cu0030'}); otherwise,
 203      * the first character of the representation of the magnitude will
 204      * not be the zero character.


 369         int radix = 1 << shift;
 370         int mask = radix - 1;
 371         do {
 372             buf[--charPos] = Integer.digits[val & mask];
 373             val >>>= shift;
 374         } while (charPos > offset);
 375     }
 376 
 377     /** byte[]/LATIN1 version    */
 378     static void formatUnsignedInt(int val, int shift, byte[] buf, int offset, int len) {
 379         int charPos = offset + len;
 380         int radix = 1 << shift;
 381         int mask = radix - 1;
 382         do {
 383             buf[--charPos] = (byte)Integer.digits[val & mask];
 384             val >>>= shift;
 385         } while (charPos > offset);
 386     }
 387 
 388     /** byte[]/UTF16 version    */
 389     private static void formatUnsignedIntUTF16(int val, int shift, byte[] buf, int offset, int len) {
 390         int charPos = offset + len;
 391         int radix = 1 << shift;
 392         int mask = radix - 1;
 393         do {
 394             StringUTF16.Trusted.putChar(buf, --charPos, Integer.digits[val & mask]);
 395             val >>>= shift;
 396         } while (charPos > offset);
 397     }
 398 
 399     static final byte[] DigitTens = {
 400         '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
 401         '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
 402         '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
 403         '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
 404         '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
 405         '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
 406         '6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
 407         '7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
 408         '8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
 409         '9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
 410         } ;
 411 
 412     static final byte[] DigitOnes = {
 413         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 414         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',


 425 
 426     /**
 427      * Returns a {@code String} object representing the
 428      * specified integer. The argument is converted to signed decimal
 429      * representation and returned as a string, exactly as if the
 430      * argument and radix 10 were given as arguments to the {@link
 431      * #toString(int, int)} method.
 432      *
 433      * @param   i   an integer to be converted.
 434      * @return  a string representation of the argument in base&nbsp;10.
 435      */
 436     @HotSpotIntrinsicCandidate
 437     public static String toString(int i) {
 438         int size = stringSize(i);
 439         if (COMPACT_STRINGS) {
 440             byte[] buf = new byte[size];
 441             getChars(i, size, buf);
 442             return new String(buf, LATIN1);
 443         } else {
 444             byte[] buf = new byte[size * 2];
 445             StringUTF16.Trusted.getChars(i, size, buf);
 446             return new String(buf, UTF16);
 447         }
 448     }
 449 
 450     /**
 451      * Returns a string representation of the argument as an unsigned
 452      * decimal value.
 453      *
 454      * The argument is converted to unsigned decimal representation
 455      * and returned as a string exactly as if the argument and radix
 456      * 10 were given as arguments to the {@link #toUnsignedString(int,
 457      * int)} method.
 458      *
 459      * @param   i  an integer to be converted to an unsigned string.
 460      * @return  an unsigned string representation of the argument.
 461      * @see     #toUnsignedString(int, int)
 462      * @since 1.8
 463      */
 464     public static String toUnsignedString(int i) {
 465         return Long.toString(toUnsignedLong(i));


 495         while (i <= -100) {
 496             q = i / 100;
 497             r = (q * 100) - i;
 498             i = q;
 499             buf[--charPos] = DigitOnes[r];
 500             buf[--charPos] = DigitTens[r];
 501         }
 502 
 503         // We know there are at most two digits left at this point.
 504         q = i / 10;
 505         r = (q * 10) - i;
 506         buf[--charPos] = (byte)('0' + r);
 507 
 508         // Whatever left is the remaining digit.
 509         if (q < 0) {
 510             buf[--charPos] = (byte)('0' - q);
 511         }
 512 
 513         if (negative) {
 514             buf[--charPos] = (byte)'-';











































 515         }
 516         return charPos;
 517     }
 518 
 519     // Left here for compatibility reasons, see JDK-8143900.
 520     static final int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
 521                                       99999999, 999999999, Integer.MAX_VALUE };
 522 
 523     /**
 524      * Returns the string representation size for a given int value.
 525      *
 526      * @param x int value
 527      * @return string size
 528      *
 529      * @implNote There are other ways to compute this: e.g. binary search,
 530      * but values are biased heavily towards zero, and therefore linear search
 531      * wins. The iteration results are also routinely inlined in the generated
 532      * code after loop unrolling.
 533      */
 534     static int stringSize(int x) {


< prev index next >