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

Print this page
rev 7120 : 8007398: Peformance improvements to Integer and Long string formatting.
Reviewed-by: mdugiou, martin
Contributed-by: Steven Schlansker <stevenschlansker@gmail.com>, Mike Duigou <mike.duigou@oracle.com>


   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


 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


 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',


 858      * Returns the value of this {@code Integer} as a {@code short}
 859      * after a narrowing primitive conversion.
 860      * @jls 5.1.3 Narrowing Primitive Conversions
 861      */
 862     public short shortValue() {
 863         return (short)value;
 864     }
 865 
 866     /**
 867      * Returns the value of this {@code Integer} as an
 868      * {@code int}.
 869      */
 870     public int intValue() {
 871         return value;
 872     }
 873 
 874     /**
 875      * Returns the value of this {@code Integer} as a {@code long}
 876      * after a widening primitive conversion.
 877      * @jls 5.1.2 Widening Primitive Conversions

 878      */
 879     public long longValue() {
 880         return (long)value;
 881     }
 882 
 883     /**
 884      * Returns the value of this {@code Integer} as a {@code float}
 885      * after a widening primitive conversion.
 886      * @jls 5.1.2 Widening Primitive Conversions
 887      */
 888     public float floatValue() {
 889         return (float)value;
 890     }
 891 
 892     /**
 893      * Returns the value of this {@code Integer} as a {@code double}
 894      * after a widening primitive conversion.
 895      * @jls 5.1.2 Widening Primitive Conversions
 896      */
 897     public double doubleValue() {




   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 
  30 /**
  31  * The {@code Integer} class wraps a value of the primitive type
  32  * {@code int} in an object. An object of type {@code Integer}
  33  * contains a single field whose type is {@code int}.
  34  *
  35  * <p>In addition, this class provides several methods for converting
  36  * an {@code int} to a {@code String} and a {@code String} to an
  37  * {@code int}, as well as other constants and methods useful when
  38  * dealing with an {@code int}.
  39  *
  40  * <p>Implementation note: The implementations of the "bit twiddling"
  41  * methods (such as {@link #highestOneBit(int) highestOneBit} and
  42  * {@link #numberOfTrailingZeros(int) numberOfTrailingZeros}) are
  43  * based on material from Henry S. Warren, Jr.'s <i>Hacker's
  44  * Delight</i>, (Addison Wesley, 2002).
  45  *
  46  * @author  Lee Boynton
  47  * @author  Arthur van Hoff
  48  * @author  Josh Bloch


 167      * {@code 10} is used instead.
 168      *
 169      * <p>Note that since the first argument is treated as an unsigned
 170      * value, no leading sign character is printed.
 171      *
 172      * <p>If the magnitude is zero, it is represented by a single zero
 173      * character {@code '0'} ({@code '\u005Cu0030'}); otherwise,
 174      * the first character of the representation of the magnitude will
 175      * not be the zero character.
 176      *
 177      * <p>The behavior of radixes and the characters used as digits
 178      * are the same as {@link #toString(int, int) toString}.
 179      *
 180      * @param   i       an integer to be converted to an unsigned string.
 181      * @param   radix   the radix to use in the string representation.
 182      * @return  an unsigned string representation of the argument in the specified radix.
 183      * @see     #toString(int, int)
 184      * @since 1.8
 185      */
 186     public static String toUnsignedString(int i, int radix) {
 187         return Long.toUnsignedString(toUnsignedLong(i), radix);
 188     }
 189 
 190     /**
 191      * Returns a string representation of the integer argument as an
 192      * unsigned integer in base&nbsp;16.
 193      *
 194      * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
 195      * if the argument is negative; otherwise, it is equal to the
 196      * argument.  This value is converted to a string of ASCII digits
 197      * in hexadecimal (base&nbsp;16) with no extra leading
 198      * {@code 0}s.
 199      *
 200      * <p>The value of the argument can be recovered from the returned
 201      * string {@code s} by calling {@link
 202      * Integer#parseUnsignedInt(String, int)
 203      * Integer.parseUnsignedInt(s, 16)}.
 204      *
 205      * <p>If the unsigned magnitude is zero, it is represented by a
 206      * single zero character {@code '0'} ({@code '\u005Cu0030'});
 207      * otherwise, the first character of the representation of the


 289      * single zero character {@code '0'} ({@code '\u005Cu0030'});
 290      * otherwise, the first character of the representation of the
 291      * unsigned magnitude will not be the zero character. The
 292      * characters {@code '0'} ({@code '\u005Cu0030'}) and {@code
 293      * '1'} ({@code '\u005Cu0031'}) are used as binary digits.
 294      *
 295      * @param   i   an integer to be converted to a string.
 296      * @return  the string representation of the unsigned integer value
 297      *          represented by the argument in binary (base&nbsp;2).
 298      * @see #parseUnsignedInt(String, int)
 299      * @see #toUnsignedString(int, int)
 300      * @since   JDK1.0.2
 301      */
 302     public static String toBinaryString(int i) {
 303         return toUnsignedString0(i, 1);
 304     }
 305 
 306     /**
 307      * Convert the integer to an unsigned number.
 308      */
 309     private static String toUnsignedString0(int val, int shift) {
 310         // assert shift > 0 && shift <=5 : "Illegal shift value";
 311         int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val);
 312         int chars = Math.max(((mag + (shift - 1)) / shift), 1);
 313         char[] buf = new char[chars];
 314 
 315         formatUnsignedInt(val, shift, buf, 0, chars);
 316 
 317         // Use special constructor which takes over "buf".
 318         return new String(buf, true);
 319     }
 320 
 321     /**
 322      * Format a long (treated as unsigned) into a character buffer.
 323      * @param val the unsigned int to format
 324      * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
 325      * @param buf the character buffer to write to
 326      * @param offset the offset in the destination buffer to start at
 327      * @param len the number of characters to write
 328      * @return the lowest character  location used
 329      */
 330      static int formatUnsignedInt(int val, int shift, char[] buf, int offset, int len) {
 331         int charPos = len;
 332         int radix = 1 << shift;
 333         int mask = radix - 1;
 334         do {
 335             buf[offset + --charPos] = Integer.digits[val & mask];
 336             val >>>= shift;
 337         } while (val != 0 && charPos > 0);
 338 
 339         return charPos;
 340     }
 341 

 342     final static char [] DigitTens = {
 343         '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
 344         '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
 345         '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
 346         '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
 347         '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
 348         '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
 349         '6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
 350         '7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
 351         '8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
 352         '9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
 353         } ;
 354 
 355     final static char [] DigitOnes = {
 356         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 357         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 358         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 359         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 360         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 361         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',


 876      * Returns the value of this {@code Integer} as a {@code short}
 877      * after a narrowing primitive conversion.
 878      * @jls 5.1.3 Narrowing Primitive Conversions
 879      */
 880     public short shortValue() {
 881         return (short)value;
 882     }
 883 
 884     /**
 885      * Returns the value of this {@code Integer} as an
 886      * {@code int}.
 887      */
 888     public int intValue() {
 889         return value;
 890     }
 891 
 892     /**
 893      * Returns the value of this {@code Integer} as a {@code long}
 894      * after a widening primitive conversion.
 895      * @jls 5.1.2 Widening Primitive Conversions
 896      * @see Integer#toUnsignedLong(int)
 897      */
 898     public long longValue() {
 899         return (long)value;
 900     }
 901 
 902     /**
 903      * Returns the value of this {@code Integer} as a {@code float}
 904      * after a widening primitive conversion.
 905      * @jls 5.1.2 Widening Primitive Conversions
 906      */
 907     public float floatValue() {
 908         return (float)value;
 909     }
 910 
 911     /**
 912      * Returns the value of this {@code Integer} as a {@code double}
 913      * after a widening primitive conversion.
 914      * @jls 5.1.2 Widening Primitive Conversions
 915      */
 916     public double doubleValue() {