151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/* 22c87ad3a45cecf9e344487cad1abfdebe79f2c7cNarayan Kamath * Copyright (C) 2014 The Android Open Source Project 3cf86f3319e738c76c5e97ad95bb8a4e30654a787Yi Kong * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. 451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is free software; you can redistribute it and/or modify it 751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * under the terms of the GNU General Public License version 2 only, as 851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * published by the Free Software Foundation. Oracle designates this 951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular file as subject to the "Classpath" exception as provided 1051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Oracle in the LICENSE file that accompanied this code. 1151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 1251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is distributed in the hope that it will be useful, but WITHOUT 1351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * version 2 for more details (a copy is included in the LICENSE file that 1651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * accompanied this code). 1751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 1851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * You should have received a copy of the GNU General Public License version 1951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2 along with this work; if not, write to the Free Software Foundation, 2051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or visit www.oracle.com if you need additional information or have any 2451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * questions. 2551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 2651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipackage java.lang; 2851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 296a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniakimport java.lang.annotation.Native; 3051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 3151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/** 3251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The {@code Integer} class wraps a value of the primitive type 3351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code int} in an object. An object of type {@code Integer} 3451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * contains a single field whose type is {@code int}. 3551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 3651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>In addition, this class provides several methods for converting 3751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an {@code int} to a {@code String} and a {@code String} to an 3851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code int}, as well as other constants and methods useful when 3951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * dealing with an {@code int}. 4051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 4151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: The implementations of the "bit twiddling" 4251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * methods (such as {@link #highestOneBit(int) highestOneBit} and 4351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link #numberOfTrailingZeros(int) numberOfTrailingZeros}) are 4451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * based on material from Henry S. Warren, Jr.'s <i>Hacker's 4551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Delight</i>, (Addison Wesley, 2002). 4651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 4751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author Lee Boynton 4851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author Arthur van Hoff 4951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author Josh Bloch 5051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author Joseph D. Darcy 5151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.0 5251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 5351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipublic final class Integer extends Number implements Comparable<Integer> { 5451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 5551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A constant holding the minimum value an {@code int} can 5651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * have, -2<sup>31</sup>. 5751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 586a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak @Native public static final int MIN_VALUE = 0x80000000; 5951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 6051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 6151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A constant holding the maximum value an {@code int} can 6251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * have, 2<sup>31</sup>-1. 6351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 646a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak @Native public static final int MAX_VALUE = 0x7fffffff; 6551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 6651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 6751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The {@code Class} instance representing the primitive type 6851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code int}. 6951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 7051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 7151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 726a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak @SuppressWarnings("unchecked") 7346e3649f898305d185fc40bae2542e933a16b619Piotr Jastrzebski public static final Class<Integer> TYPE = (Class<Integer>) int[].class.getComponentType(); 7451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 7551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 7651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * All possible chars for representing a number as a String 7751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 7851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski final static char[] digits = { 7951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski '0' , '1' , '2' , '3' , '4' , '5' , 8051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski '6' , '7' , '8' , '9' , 'a' , 'b' , 8151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 8251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 'i' , 'j' , 'k' , 'l' , 'm' , 'n' , 8351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 'o' , 'p' , 'q' , 'r' , 's' , 't' , 8451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 'u' , 'v' , 'w' , 'x' , 'y' , 'z' 8551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski }; 8651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 8751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 8851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a string representation of the first argument in the 8951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * radix specified by the second argument. 9051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 9151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If the radix is smaller than {@code Character.MIN_RADIX} 9251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or larger than {@code Character.MAX_RADIX}, then the radix 9351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 10} is used instead. 9451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 9551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If the first argument is negative, the first element of the 9651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * result is the ASCII minus character {@code '-'} 976a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * ({@code '\u005Cu002D'}). If the first argument is not 9851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * negative, no sign character appears in the result. 9951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 10051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The remaining characters of the result represent the magnitude 10151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the first argument. If the magnitude is zero, it is 10251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by a single zero character {@code '0'} 1036a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * ({@code '\u005Cu0030'}); otherwise, the first character of 10451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the representation of the magnitude will not be the zero 10551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * character. The following ASCII characters are used as digits: 10651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 10751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 10851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0123456789abcdefghijklmnopqrstuvwxyz} 10951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 11051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 1116a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * These are {@code '\u005Cu0030'} through 1126a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * {@code '\u005Cu0039'} and {@code '\u005Cu0061'} through 1136a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * {@code '\u005Cu007A'}. If {@code radix} is 11451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <var>N</var>, then the first <var>N</var> of these characters 11551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are used as radix-<var>N</var> digits in the order shown. Thus, 11651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the digits for hexadecimal (radix 16) are 11751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0123456789abcdef}. If uppercase letters are 11851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * desired, the {@link java.lang.String#toUpperCase()} method may 11951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * be called on the result: 12051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 12151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 12251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Integer.toString(n, 16).toUpperCase()} 12351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 12451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 12551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param i an integer to be converted to a string. 12651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param radix the radix to use in the string representation. 12751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a string representation of the argument in the specified radix. 12851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Character#MAX_RADIX 12951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Character#MIN_RADIX 13051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 13151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static String toString(int i, int radix) { 13251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) 13351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski radix = 10; 13451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 13551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* Use the faster version */ 13651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (radix == 10) { 13751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return toString(i); 13851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 13951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 14051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski char buf[] = new char[33]; 14151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski boolean negative = (i < 0); 14251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int charPos = 32; 14351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 14451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (!negative) { 14551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski i = -i; 14651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 14751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 14851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (i <= -radix) { 1496a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak buf[charPos--] = digits[-(i % radix)]; 1506a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak i = i / radix; 15151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 15251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf[charPos] = digits[-i]; 15351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 15451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (negative) { 15551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf[--charPos] = '-'; 15651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 15751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 15851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return new String(buf, charPos, (33 - charPos)); 15951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 16051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 16151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 162e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * Returns a string representation of the first argument as an 163e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * unsigned integer value in the radix specified by the second 164e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * argument. 165e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * 166e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * <p>If the radix is smaller than {@code Character.MIN_RADIX} 167e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * or larger than {@code Character.MAX_RADIX}, then the radix 168e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * {@code 10} is used instead. 169e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * 170e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * <p>Note that since the first argument is treated as an unsigned 171e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * value, no leading sign character is printed. 172e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * 173e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * <p>If the magnitude is zero, it is represented by a single zero 174e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * character {@code '0'} ({@code '\u005Cu0030'}); otherwise, 175e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * the first character of the representation of the magnitude will 176e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * not be the zero character. 177e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * 178e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * <p>The behavior of radixes and the characters used as digits 179e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * are the same as {@link #toString(int, int) toString}. 180e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * 181e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @param i an integer to be converted to an unsigned string. 182e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @param radix the radix to use in the string representation. 183e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @return an unsigned string representation of the argument in the specified radix. 184e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @see #toString(int, int) 185e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @since 1.8 186e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong */ 187e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong public static String toUnsignedString(int i, int radix) { 188e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong return Long.toUnsignedString(toUnsignedLong(i), radix); 189e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong } 190e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong 191e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong /** 19251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a string representation of the integer argument as an 19351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * unsigned integer in base 16. 19451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 19551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The unsigned integer value is the argument plus 2<sup>32</sup> 19651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the argument is negative; otherwise, it is equal to the 19751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument. This value is converted to a string of ASCII digits 19851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * in hexadecimal (base 16) with no extra leading 1996a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * {@code 0}s. 2006a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * 2016a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * <p>The value of the argument can be recovered from the returned 2026a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * string {@code s} by calling {@link 2036a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * Integer#parseUnsignedInt(String, int) 2046a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * Integer.parseUnsignedInt(s, 16)}. 2056a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * 2066a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * <p>If the unsigned magnitude is zero, it is represented by a 2076a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * single zero character {@code '0'} ({@code '\u005Cu0030'}); 2086a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * otherwise, the first character of the representation of the 2096a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * unsigned magnitude will not be the zero character. The 2106a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * following characters are used as hexadecimal digits: 21151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 21251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 21351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0123456789abcdef} 21451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 21551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2166a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * These are the characters {@code '\u005Cu0030'} through 2176a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * {@code '\u005Cu0039'} and {@code '\u005Cu0061'} through 2186a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * {@code '\u005Cu0066'}. If uppercase letters are 21951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * desired, the {@link java.lang.String#toUpperCase()} method may 22051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * be called on the result: 22151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 22251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 22351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Integer.toHexString(n).toUpperCase()} 22451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 22551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 22651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param i an integer to be converted to a string. 22751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the string representation of the unsigned integer value 22851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by the argument in hexadecimal (base 16). 2296a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @see #parseUnsignedInt(String, int) 2306a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @see #toUnsignedString(int, int) 23151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.0.2 23251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 23351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static String toHexString(int i) { 234e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong return toUnsignedString0(i, 4); 23551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 23651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 23751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 23851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a string representation of the integer argument as an 23951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * unsigned integer in base 8. 24051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 24151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The unsigned integer value is the argument plus 2<sup>32</sup> 24251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the argument is negative; otherwise, it is equal to the 24351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument. This value is converted to a string of ASCII digits 24451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * in octal (base 8) with no extra leading {@code 0}s. 24551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2466a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * <p>The value of the argument can be recovered from the returned 2476a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * string {@code s} by calling {@link 2486a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * Integer#parseUnsignedInt(String, int) 2496a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * Integer.parseUnsignedInt(s, 8)}. 2506a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * 25151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If the unsigned magnitude is zero, it is represented by a 2526a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * single zero character {@code '0'} ({@code '\u005Cu0030'}); 2536a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * otherwise, the first character of the representation of the 2546a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * unsigned magnitude will not be the zero character. The 2556a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * following characters are used as octal digits: 25651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 25751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 25851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 01234567} 25951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 26051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2616a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * These are the characters {@code '\u005Cu0030'} through 2626a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * {@code '\u005Cu0037'}. 26351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 26451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param i an integer to be converted to a string. 26551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the string representation of the unsigned integer value 26651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by the argument in octal (base 8). 2676a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @see #parseUnsignedInt(String, int) 2686a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @see #toUnsignedString(int, int) 26951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.0.2 27051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 27151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static String toOctalString(int i) { 272e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong return toUnsignedString0(i, 3); 27351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 27451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 27551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 27651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a string representation of the integer argument as an 27751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * unsigned integer in base 2. 27851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 27951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The unsigned integer value is the argument plus 2<sup>32</sup> 28051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the argument is negative; otherwise it is equal to the 28151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument. This value is converted to a string of ASCII digits 28251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * in binary (base 2) with no extra leading {@code 0}s. 2836a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * 2846a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * <p>The value of the argument can be recovered from the returned 2856a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * string {@code s} by calling {@link 2866a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * Integer#parseUnsignedInt(String, int) 2876a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * Integer.parseUnsignedInt(s, 2)}. 2886a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * 2896a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * <p>If the unsigned magnitude is zero, it is represented by a 2906a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * single zero character {@code '0'} ({@code '\u005Cu0030'}); 2916a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * otherwise, the first character of the representation of the 2926a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * unsigned magnitude will not be the zero character. The 2936a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * characters {@code '0'} ({@code '\u005Cu0030'}) and {@code 2946a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * '1'} ({@code '\u005Cu0031'}) are used as binary digits. 29551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 29651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param i an integer to be converted to a string. 29751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the string representation of the unsigned integer value 29851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by the argument in binary (base 2). 2996a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @see #parseUnsignedInt(String, int) 3006a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @see #toUnsignedString(int, int) 30151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.0.2 30251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 30351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static String toBinaryString(int i) { 304e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong return toUnsignedString0(i, 1); 30551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 30651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 30751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 30851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Convert the integer to an unsigned number. 30951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 3106a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak private static String toUnsignedString0(int val, int shift) { 3116a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak // assert shift > 0 && shift <=5 : "Illegal shift value"; 3126a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val); 3136a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak int chars = Math.max(((mag + (shift - 1)) / shift), 1); 3146a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak char[] buf = new char[chars]; 3156a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak 3166a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak formatUnsignedInt(val, shift, buf, 0, chars); 3176a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak 3186a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak // Use special constructor which takes over "buf". 3196a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak return new String(buf); 3206a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak } 3216a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak 3226a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak /** 3236a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * Format a long (treated as unsigned) into a character buffer. 3246a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @param val the unsigned int to format 3256a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary) 3266a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @param buf the character buffer to write to 3276a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @param offset the offset in the destination buffer to start at 3286a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @param len the number of characters to write 3296a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @return the lowest character location used 3306a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak */ 3316a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak static int formatUnsignedInt(int val, int shift, char[] buf, int offset, int len) { 3326a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak int charPos = len; 33351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int radix = 1 << shift; 33451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int mask = radix - 1; 33551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski do { 3366a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak buf[offset + --charPos] = Integer.digits[val & mask]; 3376a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak val >>>= shift; 3386a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak } while (val != 0 && charPos > 0); 33951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 3406a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak return charPos; 34151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 34251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 3439ad355852dacfe563b49b543f37d843ae6e94f06Yi Kong private static final String[] SMALL_NEG_VALUES = new String[100]; 3449ad355852dacfe563b49b543f37d843ae6e94f06Yi Kong private static final String[] SMALL_NONNEG_VALUES = new String[100]; 34551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 34651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski final static char [] DigitTens = { 34751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', 34851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', 34951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', 35051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', 35151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', 35251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', 35351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', 35451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', 35551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', 35651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski '9', '9', '9', '9', '9', '9', '9', '9', '9', '9', 35751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } ; 35851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 35951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski final static char [] DigitOnes = { 36051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 36151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 36251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 36351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 36451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 36551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 36651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 36751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 36851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 36951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 37051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } ; 37151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 37251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // I use the "invariant division by multiplication" trick to 37351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // accelerate Integer.toString. In particular we want to 37451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // avoid division by 10. 37551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // 37651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // The "trick" has roughly the same performance characteristics 37751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // as the "classic" Integer.toString code on a non-JIT VM. 37851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // The trick avoids .rem and .div calls but has a longer code 37951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // path and is thus dominated by dispatch overhead. In the 38051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // JIT case the dispatch overhead doesn't exist and the 38151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // "trick" is considerably faster than the classic code. 38251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // 38351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // TODO-FIXME: convert (x * 52429) into the equiv shift-add 38451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // sequence. 38551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // 38651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // RE: Division by Invariant Integers using Multiplication 38751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // T Gralund, P Montgomery 38851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // ACM PLDI 1994 38951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // 39051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 39151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 39251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code String} object representing the 39351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified integer. The argument is converted to signed decimal 39451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * representation and returned as a string, exactly as if the 39551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument and radix 10 were given as arguments to the {@link 39651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * #toString(int, int)} method. 39751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 39851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param i an integer to be converted. 39951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a string representation of the argument in base 10. 40051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 40151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static String toString(int i) { 40251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (i == Integer.MIN_VALUE) 40351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "-2147483648"; 4049ad355852dacfe563b49b543f37d843ae6e94f06Yi Kong 4059ad355852dacfe563b49b543f37d843ae6e94f06Yi Kong // Android-changed: cache the string literal for small values. 4069ad355852dacfe563b49b543f37d843ae6e94f06Yi Kong boolean negative = i < 0; 4079ad355852dacfe563b49b543f37d843ae6e94f06Yi Kong boolean small = negative ? i > -100 : i < 100; 4089ad355852dacfe563b49b543f37d843ae6e94f06Yi Kong if (small) { 4099ad355852dacfe563b49b543f37d843ae6e94f06Yi Kong final String[] smallValues = negative ? SMALL_NEG_VALUES : SMALL_NONNEG_VALUES; 4109ad355852dacfe563b49b543f37d843ae6e94f06Yi Kong 4119ad355852dacfe563b49b543f37d843ae6e94f06Yi Kong if (negative) { 4129ad355852dacfe563b49b543f37d843ae6e94f06Yi Kong i = -i; 4139ad355852dacfe563b49b543f37d843ae6e94f06Yi Kong if (smallValues[i] == null) { 4149ad355852dacfe563b49b543f37d843ae6e94f06Yi Kong smallValues[i] = 4159ad355852dacfe563b49b543f37d843ae6e94f06Yi Kong i < 10 ? new String(new char[]{'-', DigitOnes[i]}) 4169ad355852dacfe563b49b543f37d843ae6e94f06Yi Kong : new String(new char[]{'-', DigitTens[i], DigitOnes[i]}); 4179ad355852dacfe563b49b543f37d843ae6e94f06Yi Kong } 4189ad355852dacfe563b49b543f37d843ae6e94f06Yi Kong } else { 4199ad355852dacfe563b49b543f37d843ae6e94f06Yi Kong if (smallValues[i] == null) { 4209ad355852dacfe563b49b543f37d843ae6e94f06Yi Kong smallValues[i] = 4219ad355852dacfe563b49b543f37d843ae6e94f06Yi Kong i < 10 ? new String(new char[]{DigitOnes[i]}) 4229ad355852dacfe563b49b543f37d843ae6e94f06Yi Kong : new String(new char[]{DigitTens[i], DigitOnes[i]}); 4239ad355852dacfe563b49b543f37d843ae6e94f06Yi Kong } 4249ad355852dacfe563b49b543f37d843ae6e94f06Yi Kong } 4259ad355852dacfe563b49b543f37d843ae6e94f06Yi Kong return smallValues[i]; 4269ad355852dacfe563b49b543f37d843ae6e94f06Yi Kong } 4279ad355852dacfe563b49b543f37d843ae6e94f06Yi Kong 4289ad355852dacfe563b49b543f37d843ae6e94f06Yi Kong int size = negative ? stringSize(-i) + 1 : stringSize(i); 42951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski char[] buf = new char[size]; 43051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski getChars(i, size, buf); 431b90c36f25b990336a456daad69ea9db9256b86cePrzemyslaw Szczepaniak return new String(buf); 43251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 43351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 43451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 435e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * Returns a string representation of the argument as an unsigned 436e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * decimal value. 437e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * 438e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * The argument is converted to unsigned decimal representation 439e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * and returned as a string exactly as if the argument and radix 440e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * 10 were given as arguments to the {@link #toUnsignedString(int, 441e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * int)} method. 442e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * 443e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @param i an integer to be converted to an unsigned string. 444e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @return an unsigned string representation of the argument. 445e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @see #toUnsignedString(int, int) 446e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @since 1.8 447e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong */ 448e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong public static String toUnsignedString(int i) { 449e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong return Long.toString(toUnsignedLong(i)); 450e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong } 451e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong 452e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong /** 45351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Places characters representing the integer i into the 45451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * character array buf. The characters are placed into 45551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the buffer backwards starting with the least significant 45651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * digit at the specified index (exclusive), and working 45751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * backwards from there. 45851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 45951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Will fail if i == Integer.MIN_VALUE 46051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 46151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski static void getChars(int i, int index, char[] buf) { 46251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int q, r; 46351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int charPos = index; 46451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski char sign = 0; 46551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 46651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (i < 0) { 46751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski sign = '-'; 46851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski i = -i; 46951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 47051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 47151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Generate two digits per iteration 47251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (i >= 65536) { 47351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski q = i / 100; 47451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // really: r = i - (q * 100); 47551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski r = i - ((q << 6) + (q << 5) + (q << 2)); 47651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski i = q; 47751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf [--charPos] = DigitOnes[r]; 47851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf [--charPos] = DigitTens[r]; 47951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 48051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 48151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Fall thru to fast mode for smaller numbers 48251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // assert(i <= 65536, i); 48351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (;;) { 48451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski q = (i * 52429) >>> (16+3); 48551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ... 48651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf [--charPos] = digits [r]; 48751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski i = q; 48851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (i == 0) break; 48951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 49051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (sign != 0) { 49151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf [--charPos] = sign; 49251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 49351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 49451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 49551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999, 49651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 99999999, 999999999, Integer.MAX_VALUE }; 49751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 49851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Requires positive x 49951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski static int stringSize(int x) { 50051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i=0; ; i++) 50151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (x <= sizeTable[i]) 50251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return i+1; 50351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 50451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 50551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 50651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Parses the string argument as a signed integer in the radix 50751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified by the second argument. The characters in the string 50851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * must all be digits of the specified radix (as determined by 50951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * whether {@link java.lang.Character#digit(char, int)} returns a 51051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * nonnegative value), except that the first character may be an 5116a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * ASCII minus sign {@code '-'} ({@code '\u005Cu002D'}) to 51251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * indicate a negative value or an ASCII plus sign {@code '+'} 5136a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * ({@code '\u005Cu002B'}) to indicate a positive value. The 51451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * resulting integer value is returned. 51551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 51651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>An exception of type {@code NumberFormatException} is 51751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * thrown if any of the following situations occurs: 51851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 51951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>The first argument is {@code null} or is a string of 52051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * length zero. 52151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 52251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>The radix is either smaller than 52351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link java.lang.Character#MIN_RADIX} or 52451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * larger than {@link java.lang.Character#MAX_RADIX}. 52551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 52651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>Any character of the string is not a digit of the specified 52751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * radix, except that the first character may be a minus sign 5286a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * {@code '-'} ({@code '\u005Cu002D'}) or plus sign 5296a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * {@code '+'} ({@code '\u005Cu002B'}) provided that the 53051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * string is longer than length 1. 53151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 53251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>The value represented by the string is not a value of type 53351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code int}. 53451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 53551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 53651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Examples: 53751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote><pre> 53851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parseInt("0", 10) returns 0 53951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parseInt("473", 10) returns 473 54051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parseInt("+42", 10) returns 42 54151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parseInt("-0", 10) returns 0 54251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parseInt("-FF", 16) returns -255 54351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parseInt("1100110", 2) returns 102 54451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parseInt("2147483647", 10) returns 2147483647 54551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parseInt("-2147483648", 10) returns -2147483648 54651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parseInt("2147483648", 10) throws a NumberFormatException 54751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parseInt("99", 8) throws a NumberFormatException 54851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parseInt("Kona", 10) throws a NumberFormatException 54951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parseInt("Kona", 27) returns 411787 55051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre></blockquote> 55151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 55251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param s the {@code String} containing the integer 55351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * representation to be parsed 55451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param radix the radix to be used while parsing {@code s}. 55551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the integer represented by the string argument in the 55651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified radix. 55751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NumberFormatException if the {@code String} 55851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * does not contain a parsable {@code int}. 55951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 56051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int parseInt(String s, int radix) 56151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws NumberFormatException 56251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski { 56351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* 56451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * WARNING: This method may be invoked early during VM initialization 56551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * before IntegerCache is initialized. Care must be taken to not use 56651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the valueOf method. 56751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 56851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 56951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (s == null) { 5709678c4c20f775334f6ac37f5c52aabf8e247c773Narayan Kamath throw new NumberFormatException("s == null"); 57151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 57251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 57351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (radix < Character.MIN_RADIX) { 57451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new NumberFormatException("radix " + radix + 57551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski " less than Character.MIN_RADIX"); 57651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 57751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 57851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (radix > Character.MAX_RADIX) { 57951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new NumberFormatException("radix " + radix + 58051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski " greater than Character.MAX_RADIX"); 58151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 58251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 58351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int result = 0; 58451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski boolean negative = false; 58551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int i = 0, len = s.length(); 58651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int limit = -Integer.MAX_VALUE; 58751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int multmin; 58851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int digit; 58951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 59051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (len > 0) { 59151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski char firstChar = s.charAt(0); 59251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (firstChar < '0') { // Possible leading "+" or "-" 59351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (firstChar == '-') { 59451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski negative = true; 59551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski limit = Integer.MIN_VALUE; 59651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } else if (firstChar != '+') 59751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw NumberFormatException.forInputString(s); 59851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 59951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (len == 1) // Cannot have lone "+" or "-" 60051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw NumberFormatException.forInputString(s); 60151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski i++; 60251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 60351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski multmin = limit / radix; 60451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (i < len) { 60551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Accumulating negatively avoids surprises near MAX_VALUE 60651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski digit = Character.digit(s.charAt(i++),radix); 60751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (digit < 0) { 60851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw NumberFormatException.forInputString(s); 60951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 61051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (result < multmin) { 61151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw NumberFormatException.forInputString(s); 61251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 61351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski result *= radix; 61451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (result < limit + digit) { 61551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw NumberFormatException.forInputString(s); 61651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 61751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski result -= digit; 61851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 61951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } else { 62051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw NumberFormatException.forInputString(s); 62151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 62251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return negative ? result : -result; 62351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 62451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 62551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 62651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Parses the string argument as a signed decimal integer. The 62751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * characters in the string must all be decimal digits, except 62851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that the first character may be an ASCII minus sign {@code '-'} 6296a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * ({@code '\u005Cu002D'}) to indicate a negative value or an 6306a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * ASCII plus sign {@code '+'} ({@code '\u005Cu002B'}) to 63151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * indicate a positive value. The resulting integer value is 63251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned, exactly as if the argument and the radix 10 were 63351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * given as arguments to the {@link #parseInt(java.lang.String, 63451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * int)} method. 63551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 63651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param s a {@code String} containing the {@code int} 63751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * representation to be parsed 63851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the integer value represented by the argument in decimal. 63951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NumberFormatException if the string does not contain a 64051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parsable integer. 64151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 64251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int parseInt(String s) throws NumberFormatException { 64351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return parseInt(s,10); 64451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 64551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 64651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 647e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * Parses the string argument as an unsigned integer in the radix 648e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * specified by the second argument. An unsigned integer maps the 649e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * values usually associated with negative numbers to positive 650e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * numbers larger than {@code MAX_VALUE}. 651e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * 652e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * The characters in the string must all be digits of the 653e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * specified radix (as determined by whether {@link 654e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * java.lang.Character#digit(char, int)} returns a nonnegative 655e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * value), except that the first character may be an ASCII plus 656e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * sign {@code '+'} ({@code '\u005Cu002B'}). The resulting 657e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * integer value is returned. 658e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * 659e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * <p>An exception of type {@code NumberFormatException} is 660e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * thrown if any of the following situations occurs: 661e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * <ul> 662e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * <li>The first argument is {@code null} or is a string of 663e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * length zero. 664e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * 665e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * <li>The radix is either smaller than 666e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * {@link java.lang.Character#MIN_RADIX} or 667e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * larger than {@link java.lang.Character#MAX_RADIX}. 668e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * 669e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * <li>Any character of the string is not a digit of the specified 670e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * radix, except that the first character may be a plus sign 671e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * {@code '+'} ({@code '\u005Cu002B'}) provided that the 672e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * string is longer than length 1. 673e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * 674e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * <li>The value represented by the string is larger than the 675e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * largest unsigned {@code int}, 2<sup>32</sup>-1. 676e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * 677e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * </ul> 678e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * 679e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * 680e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @param s the {@code String} containing the unsigned integer 681e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * representation to be parsed 682e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @param radix the radix to be used while parsing {@code s}. 683e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @return the integer represented by the string argument in the 684e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * specified radix. 685e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @throws NumberFormatException if the {@code String} 686e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * does not contain a parsable {@code int}. 687e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @since 1.8 688e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong */ 689e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong public static int parseUnsignedInt(String s, int radix) 690e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong throws NumberFormatException { 691e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong if (s == null) { 692e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong throw new NumberFormatException("null"); 693e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong } 694e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong 695e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong int len = s.length(); 696e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong if (len > 0) { 697e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong char firstChar = s.charAt(0); 698e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong if (firstChar == '-') { 699e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong throw new 700e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong NumberFormatException(String.format("Illegal leading minus sign " + 701e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong "on unsigned string %s.", s)); 702e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong } else { 703e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong if (len <= 5 || // Integer.MAX_VALUE in Character.MAX_RADIX is 6 digits 704e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong (radix == 10 && len <= 9) ) { // Integer.MAX_VALUE in base 10 is 10 digits 705e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong return parseInt(s, radix); 706e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong } else { 707e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong long ell = Long.parseLong(s, radix); 708e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong if ((ell & 0xffff_ffff_0000_0000L) == 0) { 709e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong return (int) ell; 710e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong } else { 711e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong throw new 712e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong NumberFormatException(String.format("String value %s exceeds " + 713e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong "range of unsigned int.", s)); 714e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong } 715e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong } 716e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong } 717e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong } else { 718e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong throw NumberFormatException.forInputString(s); 719e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong } 720e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong } 721e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong 722e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong /** 723e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * Parses the string argument as an unsigned decimal integer. The 724e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * characters in the string must all be decimal digits, except 725e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * that the first character may be an an ASCII plus sign {@code 726e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * '+'} ({@code '\u005Cu002B'}). The resulting integer value 727e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * is returned, exactly as if the argument and the radix 10 were 728e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * given as arguments to the {@link 729e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * #parseUnsignedInt(java.lang.String, int)} method. 730e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * 731e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @param s a {@code String} containing the unsigned {@code int} 732e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * representation to be parsed 733e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @return the unsigned integer value represented by the argument in decimal. 734e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @throws NumberFormatException if the string does not contain a 735e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * parsable unsigned integer. 736e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @since 1.8 737e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong */ 738e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong public static int parseUnsignedInt(String s) throws NumberFormatException { 739e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong return parseUnsignedInt(s, 10); 740e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong } 741e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong 742e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong /** 74351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an {@code Integer} object holding the value 74451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * extracted from the specified {@code String} when parsed 74551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * with the radix given by the second argument. The first argument 74651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is interpreted as representing a signed integer in the radix 74751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified by the second argument, exactly as if the arguments 74851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * were given to the {@link #parseInt(java.lang.String, int)} 74951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method. The result is an {@code Integer} object that 75051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents the integer value specified by the string. 75151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 75251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>In other words, this method returns an {@code Integer} 75351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object equal to the value of: 75451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 75551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 75651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code new Integer(Integer.parseInt(s, radix))} 75751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 75851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 75951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param s the string to be parsed. 76051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param radix the radix to be used in interpreting {@code s} 76151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return an {@code Integer} object holding the value 76251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by the string argument in the specified 76351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * radix. 76451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NumberFormatException if the {@code String} 76551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * does not contain a parsable {@code int}. 76651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 76751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static Integer valueOf(String s, int radix) throws NumberFormatException { 76851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return Integer.valueOf(parseInt(s,radix)); 76951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 77051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 77151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 77251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an {@code Integer} object holding the 77351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * value of the specified {@code String}. The argument is 77451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interpreted as representing a signed decimal integer, exactly 77551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * as if the argument were given to the {@link 77651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * #parseInt(java.lang.String)} method. The result is an 77751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Integer} object that represents the integer value 77851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified by the string. 77951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 78051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>In other words, this method returns an {@code Integer} 78151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object equal to the value of: 78251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 78351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 78451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code new Integer(Integer.parseInt(s))} 78551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 78651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 78751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param s the string to be parsed. 78851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return an {@code Integer} object holding the value 78951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by the string argument. 79051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NumberFormatException if the string cannot be parsed 79151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * as an integer. 79251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 79351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static Integer valueOf(String s) throws NumberFormatException { 79451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return Integer.valueOf(parseInt(s, 10)); 79551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 79651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 79751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 79851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Cache to support the object identity semantics of autoboxing for values between 79951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * -128 and 127 (inclusive) as required by JLS. 80051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 80151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The cache is initialized on first usage. The size of the cache 8026a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option. 80351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * During VM initialization, java.lang.Integer.IntegerCache.high property 80451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * may be set and saved in the private system properties in the 80551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * sun.misc.VM class. 80651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 80751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 80851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static class IntegerCache { 80951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski static final int low = -128; 81051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski static final int high; 81151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski static final Integer cache[]; 81251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 81351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski static { 81451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // high value may be configured by property 81551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int h = 127; 81651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski String integerCacheHighPropValue = 81751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); 81851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (integerCacheHighPropValue != null) { 8196a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak try { 8206a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak int i = parseInt(integerCacheHighPropValue); 8216a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak i = Math.max(i, 127); 8226a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak // Maximum array size is Integer.MAX_VALUE 8236a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak h = Math.min(i, Integer.MAX_VALUE - (-low) -1); 8246a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak } catch( NumberFormatException nfe) { 8256a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak // If the property cannot be parsed into an int, ignore it. 8266a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak } 82751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 82851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski high = h; 82951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 83051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski cache = new Integer[(high - low) + 1]; 83151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int j = low; 83251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for(int k = 0; k < cache.length; k++) 83351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski cache[k] = new Integer(j++); 8346a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak 8356a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak // range [-128, 127] must be interned (JLS7 5.1.7) 8366a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak assert IntegerCache.high >= 127; 83751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 83851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 83951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private IntegerCache() {} 84051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 84151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 84251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 84351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an {@code Integer} instance representing the specified 84451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code int} value. If a new {@code Integer} instance is not 84551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * required, this method should generally be used in preference to 84651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the constructor {@link #Integer(int)}, as this method is likely 84751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to yield significantly better space and time performance by 84851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * caching frequently requested values. 84951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 85051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This method will always cache values in the range -128 to 127, 85151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * inclusive, and may cache other values outside of this range. 85251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 85351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param i an {@code int} value. 85451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return an {@code Integer} instance representing {@code i}. 85551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 85651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 85751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static Integer valueOf(int i) { 85851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (i >= IntegerCache.low && i <= IntegerCache.high) 85951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return IntegerCache.cache[i + (-IntegerCache.low)]; 86051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return new Integer(i); 86151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 86251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 86351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 86451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The value of the {@code Integer}. 86551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 86651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @serial 86751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 86851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private final int value; 86951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 87051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 87151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Constructs a newly allocated {@code Integer} object that 87251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents the specified {@code int} value. 87351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 87451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param value the value to be represented by the 87551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Integer} object. 87651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 87751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Integer(int value) { 87851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski this.value = value; 87951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 88051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 88151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 88251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Constructs a newly allocated {@code Integer} object that 88351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents the {@code int} value indicated by the 88451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code String} parameter. The string is converted to an 88551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code int} value in exactly the manner used by the 88651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code parseInt} method for radix 10. 88751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 88851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param s the {@code String} to be converted to an 88951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Integer}. 89051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NumberFormatException if the {@code String} does not 89151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * contain a parsable integer. 89251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Integer#parseInt(java.lang.String, int) 89351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 89451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Integer(String s) throws NumberFormatException { 89551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski this.value = parseInt(s, 10); 89651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 89751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 89851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 8996a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * Returns the value of this {@code Integer} as a {@code byte} 9006a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * after a narrowing primitive conversion. 9016a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @jls 5.1.3 Narrowing Primitive Conversions 90251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 90351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public byte byteValue() { 90451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (byte)value; 90551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 90651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 90751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 9086a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * Returns the value of this {@code Integer} as a {@code short} 9096a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * after a narrowing primitive conversion. 9106a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @jls 5.1.3 Narrowing Primitive Conversions 91151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 91251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public short shortValue() { 91351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (short)value; 91451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 91551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 91651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 91751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the value of this {@code Integer} as an 91851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code int}. 91951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 92051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public int intValue() { 92151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return value; 92251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 92351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 92451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 9256a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * Returns the value of this {@code Integer} as a {@code long} 9266a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * after a widening primitive conversion. 9276a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @jls 5.1.2 Widening Primitive Conversions 9286a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @see Integer#toUnsignedLong(int) 92951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 93051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public long longValue() { 93151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (long)value; 93251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 93351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 93451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 9356a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * Returns the value of this {@code Integer} as a {@code float} 9366a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * after a widening primitive conversion. 9376a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @jls 5.1.2 Widening Primitive Conversions 93851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 93951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public float floatValue() { 94051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (float)value; 94151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 94251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 94351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 9446a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * Returns the value of this {@code Integer} as a {@code double} 9456a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * after a widening primitive conversion. 9466a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @jls 5.1.2 Widening Primitive Conversions 94751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 94851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public double doubleValue() { 94951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (double)value; 95051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 95151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 95251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 95351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code String} object representing this 95451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Integer}'s value. The value is converted to signed 95551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * decimal representation and returned as a string, exactly as if 95651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the integer value were given as an argument to the {@link 95751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.Integer#toString(int)} method. 95851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 95951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a string representation of the value of this object in 96051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * base 10. 96151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 96251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public String toString() { 96351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return toString(value); 96451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 96551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 96651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 96751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a hash code for this {@code Integer}. 96851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 96951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a hash code value for this object, equal to the 97051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive {@code int} value represented by this 97151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Integer} object. 97251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 9736a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak @Override 97451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public int hashCode() { 9756a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak return Integer.hashCode(value); 97651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 97751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 97851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 9792729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * Returns a hash code for a {@code int} value; compatible with 9802729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * {@code Integer.hashCode()}. 9812729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * 9822729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * @param value the value to hash 9832729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * @since 1.8 9842729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * 9852729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * @return a hash code value for a {@code int} value. 9862729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath */ 9872729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath public static int hashCode(int value) { 9882729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath return value; 9892729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath } 9902729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath 9912729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath /** 99251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Compares this object to the specified object. The result is 99351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code true} if and only if the argument is not 99451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code null} and is an {@code Integer} object that 99551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * contains the same {@code int} value as this object. 99651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 99751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param obj the object to compare with. 99851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if the objects are the same; 99951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false} otherwise. 100051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 100151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean equals(Object obj) { 100251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (obj instanceof Integer) { 100351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return value == ((Integer)obj).intValue(); 100451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 100551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 100651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 100751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 100851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 100951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines the integer value of the system property with the 101051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified name. 101151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 10126a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * <p>The first argument is treated as the name of a system 10136a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * property. System properties are accessible through the {@link 10146a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * java.lang.System#getProperty(java.lang.String)} method. The 101551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * string value of this property is then interpreted as an integer 10166a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * value using the grammar supported by {@link Integer#decode decode} and 10176a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * an {@code Integer} object representing this value is returned. 101851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 10196a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * <p>If there is no property with the specified name, if the 10206a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * specified name is empty or {@code null}, or if the property 10216a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * does not have the correct numeric format, then {@code null} is 10226a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * returned. 102351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 102451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>In other words, this method returns an {@code Integer} 102551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object equal to the value of: 102651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 102751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 102851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code getInteger(nm, null)} 102951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 103051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 103151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param nm property name. 103251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Integer} value of the property. 10336a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @throws SecurityException for the same reasons as 10346a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * {@link System#getProperty(String) System.getProperty} 103551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.System#getProperty(java.lang.String) 103651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.System#getProperty(java.lang.String, java.lang.String) 103751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 103851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static Integer getInteger(String nm) { 103951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return getInteger(nm, null); 104051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 104151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 104251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 104351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines the integer value of the system property with the 104451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified name. 104551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 10466a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * <p>The first argument is treated as the name of a system 10476a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * property. System properties are accessible through the {@link 104851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.System#getProperty(java.lang.String)} method. The 104951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * string value of this property is then interpreted as an integer 10506a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * value using the grammar supported by {@link Integer#decode decode} and 10516a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * an {@code Integer} object representing this value is returned. 105251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 105351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The second argument is the default value. An {@code Integer} object 105451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that represents the value of the second argument is returned if there 105551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is no property of the specified name, if the property does not have 105651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the correct numeric format, or if the specified name is empty or 105751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code null}. 105851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 105951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>In other words, this method returns an {@code Integer} object 106051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * equal to the value of: 106151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 106251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 106351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code getInteger(nm, new Integer(val))} 106451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 106551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 106651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * but in practice it may be implemented in a manner such as: 106751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 106851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote><pre> 106951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Integer result = getInteger(nm, null); 107051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * return (result == null) ? new Integer(val) : result; 107151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre></blockquote> 107251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 107351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to avoid the unnecessary allocation of an {@code Integer} 107451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object when the default value is not needed. 107551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 107651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param nm property name. 107751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val default value. 107851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Integer} value of the property. 10796a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @throws SecurityException for the same reasons as 10806a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * {@link System#getProperty(String) System.getProperty} 108151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.System#getProperty(java.lang.String) 108251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.System#getProperty(java.lang.String, java.lang.String) 108351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 108451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static Integer getInteger(String nm, int val) { 108551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Integer result = getInteger(nm, null); 108651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (result == null) ? Integer.valueOf(val) : result; 108751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 108851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 108951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 109051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the integer value of the system property with the 109151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified name. The first argument is treated as the name of a 109251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * system property. System properties are accessible through the 109351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link java.lang.System#getProperty(java.lang.String)} method. 109451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The string value of this property is then interpreted as an 10956a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * integer value, as per the {@link Integer#decode decode} method, 109651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and an {@code Integer} object representing this value is 10976a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * returned; in summary: 109851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 109951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul><li>If the property value begins with the two ASCII characters 110051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0x} or the ASCII character {@code #}, not 110151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * followed by a minus sign, then the rest of it is parsed as a 110251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * hexadecimal integer exactly as by the method 110351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link #valueOf(java.lang.String, int)} with radix 16. 110451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>If the property value begins with the ASCII character 110551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0} followed by another character, it is parsed as an 110651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * octal integer exactly as by the method 110751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link #valueOf(java.lang.String, int)} with radix 8. 110851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>Otherwise, the property value is parsed as a decimal integer 110951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * exactly as by the method {@link #valueOf(java.lang.String, int)} 111051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * with radix 10. 111151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 111251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 111351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The second argument is the default value. The default value is 111451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned if there is no property of the specified name, if the 111551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * property does not have the correct numeric format, or if the 111651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified name is empty or {@code null}. 111751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 111851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param nm property name. 111951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val default value. 112051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Integer} value of the property. 11216a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @throws SecurityException for the same reasons as 11226a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * {@link System#getProperty(String) System.getProperty} 11236a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @see System#getProperty(java.lang.String) 11246a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @see System#getProperty(java.lang.String, java.lang.String) 112551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 112651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static Integer getInteger(String nm, Integer val) { 112751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski String v = null; 112851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski try { 112951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski v = System.getProperty(nm); 11306a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak } catch (IllegalArgumentException | NullPointerException e) { 113151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 113251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (v != null) { 113351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski try { 113451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return Integer.decode(v); 113551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } catch (NumberFormatException e) { 113651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 113751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 113851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return val; 113951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 114051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 114151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 114251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Decodes a {@code String} into an {@code Integer}. 114351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Accepts decimal, hexadecimal, and octal numbers given 114451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by the following grammar: 114551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 114651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 114751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dl> 114851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dt><i>DecodableString:</i> 114951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i> 115051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i> 115151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i> 115251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i> 115351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i> 11546a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * 115551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dt><i>Sign:</i> 115651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dd>{@code -} 115751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <dd>{@code +} 115851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </dl> 115951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 116051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 116151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i> 116251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are as defined in section 3.10.1 of 116351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <cite>The Java™ Language Specification</cite>, 116451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * except that underscores are not accepted between digits. 116551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 116651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The sequence of characters following an optional 116751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * sign and/or radix specifier ("{@code 0x}", "{@code 0X}", 116851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * "{@code #}", or leading zero) is parsed as by the {@code 116951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Integer.parseInt} method with the indicated radix (10, 16, or 117051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 8). This sequence of characters must represent a positive 117151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * value or a {@link NumberFormatException} will be thrown. The 117251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * result is negated if first character of the specified {@code 117351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * String} is the minus sign. No whitespace characters are 117451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * permitted in the {@code String}. 117551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 117651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param nm the {@code String} to decode. 117751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return an {@code Integer} object holding the {@code int} 117851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * value represented by {@code nm} 117951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NumberFormatException if the {@code String} does not 118051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * contain a parsable integer. 118151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Integer#parseInt(java.lang.String, int) 118251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 118351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static Integer decode(String nm) throws NumberFormatException { 118451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int radix = 10; 118551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int index = 0; 118651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski boolean negative = false; 118751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Integer result; 118851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 118951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (nm.length() == 0) 119051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new NumberFormatException("Zero length string"); 119151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski char firstChar = nm.charAt(0); 119251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Handle sign, if present 119351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (firstChar == '-') { 119451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski negative = true; 119551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski index++; 119651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } else if (firstChar == '+') 119751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski index++; 119851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 119951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Handle radix specifier, if present 120051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) { 120151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski index += 2; 120251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski radix = 16; 120351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 120451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (nm.startsWith("#", index)) { 120551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski index ++; 120651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski radix = 16; 120751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 120851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (nm.startsWith("0", index) && nm.length() > 1 + index) { 120951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski index ++; 121051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski radix = 8; 121151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 121251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 121351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (nm.startsWith("-", index) || nm.startsWith("+", index)) 121451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new NumberFormatException("Sign character in wrong position"); 121551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 121651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski try { 121751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski result = Integer.valueOf(nm.substring(index), radix); 121851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski result = negative ? Integer.valueOf(-result.intValue()) : result; 121951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } catch (NumberFormatException e) { 122051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // If number is Integer.MIN_VALUE, we'll end up here. The next line 122151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // handles this case, and causes any genuine format error to be 122251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // rethrown. 122351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski String constant = negative ? ("-" + nm.substring(index)) 122451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski : nm.substring(index); 122551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski result = Integer.valueOf(constant, radix); 122651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 122751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return result; 122851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 122951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 123051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 123151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Compares two {@code Integer} objects numerically. 123251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 123351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param anotherInteger the {@code Integer} to be compared. 123451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the value {@code 0} if this {@code Integer} is 123551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * equal to the argument {@code Integer}; a value less than 123651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0} if this {@code Integer} is numerically less 123751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * than the argument {@code Integer}; and a value greater 123851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * than {@code 0} if this {@code Integer} is numerically 123951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * greater than the argument {@code Integer} (signed 124051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * comparison). 124151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.2 124251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 124351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public int compareTo(Integer anotherInteger) { 124451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return compare(this.value, anotherInteger.value); 124551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 124651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 124751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 124851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Compares two {@code int} values numerically. 124951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The value returned is identical to what would be returned by: 125051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <pre> 125151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Integer.valueOf(x).compareTo(Integer.valueOf(y)) 125251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre> 125351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 125451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param x the first {@code int} to compare 125551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param y the second {@code int} to compare 125651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the value {@code 0} if {@code x == y}; 125751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * a value less than {@code 0} if {@code x < y}; and 125851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * a value greater than {@code 0} if {@code x > y} 125951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.7 126051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 126151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int compare(int x, int y) { 126251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (x < y) ? -1 : ((x == y) ? 0 : 1); 126351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 126451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 1265e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong /** 1266e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * Compares two {@code int} values numerically treating the values 1267e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * as unsigned. 1268e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * 1269e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @param x the first {@code int} to compare 1270e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @param y the second {@code int} to compare 1271e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @return the value {@code 0} if {@code x == y}; a value less 1272e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * than {@code 0} if {@code x < y} as unsigned values; and 1273e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * a value greater than {@code 0} if {@code x > y} as 1274e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * unsigned values 1275e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @since 1.8 1276e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong */ 1277e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong public static int compareUnsigned(int x, int y) { 1278e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong return compare(x + MIN_VALUE, y + MIN_VALUE); 1279e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong } 1280e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong 1281e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong /** 1282e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * Converts the argument to a {@code long} by an unsigned 1283e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * conversion. In an unsigned conversion to a {@code long}, the 1284e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * high-order 32 bits of the {@code long} are zero and the 1285e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * low-order 32 bits are equal to the bits of the integer 1286e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * argument. 1287e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * 1288e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * Consequently, zero and positive {@code int} values are mapped 1289e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * to a numerically equal {@code long} value and negative {@code 1290e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * int} values are mapped to a {@code long} value equal to the 1291e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * input plus 2<sup>32</sup>. 1292e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * 1293e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @param x the value to convert to an unsigned {@code long} 1294e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @return the argument converted to {@code long} by an unsigned 1295e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * conversion 1296e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @since 1.8 1297e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong */ 1298e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong public static long toUnsignedLong(int x) { 1299e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong return ((long) x) & 0xffffffffL; 1300e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong } 1301e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong 1302e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong /** 1303e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * Returns the unsigned quotient of dividing the first argument by 1304e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * the second where each argument and the result is interpreted as 1305e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * an unsigned value. 1306e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * 1307e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * <p>Note that in two's complement arithmetic, the three other 1308e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * basic arithmetic operations of add, subtract, and multiply are 1309e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * bit-wise identical if the two operands are regarded as both 1310e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * being signed or both being unsigned. Therefore separate {@code 1311e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * addUnsigned}, etc. methods are not provided. 1312e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * 1313e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @param dividend the value to be divided 1314e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @param divisor the value doing the dividing 1315e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @return the unsigned quotient of the first argument divided by 1316e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * the second argument 1317e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @see #remainderUnsigned 1318e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @since 1.8 1319e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong */ 1320e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong public static int divideUnsigned(int dividend, int divisor) { 1321e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong // In lieu of tricky code, for now just use long arithmetic. 1322e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong return (int)(toUnsignedLong(dividend) / toUnsignedLong(divisor)); 1323e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong } 1324e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong 1325e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong /** 1326e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * Returns the unsigned remainder from dividing the first argument 1327e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * by the second where each argument and the result is interpreted 1328e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * as an unsigned value. 1329e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * 1330e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @param dividend the value to be divided 1331e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @param divisor the value doing the dividing 1332e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @return the unsigned remainder of the first argument divided by 1333e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * the second argument 1334e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @see #divideUnsigned 1335e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong * @since 1.8 1336e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong */ 1337e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong public static int remainderUnsigned(int dividend, int divisor) { 1338e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong // In lieu of tricky code, for now just use long arithmetic. 1339e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong return (int)(toUnsignedLong(dividend) % toUnsignedLong(divisor)); 1340e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong } 1341e2f147b9b14c7645f29e92758f811a18258feef4Yi Kong 134251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 134351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Bit twiddling 134451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 134551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 134651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The number of bits used to represent an {@code int} value in two's 134751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * complement binary form. 134851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 134951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 135051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 13516a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak @Native public static final int SIZE = 32; 13522729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath 13532729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath /** 13542729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * The number of bytes used to represent a {@code int} value in two's 13552729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * complement binary form. 13562729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * 13572729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * @since 1.8 13582729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath */ 13592729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath public static final int BYTES = SIZE / Byte.SIZE; 13602729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath 136151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 136251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an {@code int} value with at most a single one-bit, in the 136351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * position of the highest-order ("leftmost") one-bit in the specified 136451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code int} value. Returns zero if the specified value has no 136551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one-bits in its two's complement binary representation, that is, if it 136651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is equal to zero. 136751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 13686a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @param i the value whose highest one bit is to be computed 136951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return an {@code int} value with a single one-bit, in the position 137051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the highest-order one-bit in the specified value, or zero if 137151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified value is itself equal to zero. 137251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 137351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 137451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int highestOneBit(int i) { 137551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // HD, Figure 3-1 137651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski i |= (i >> 1); 137751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski i |= (i >> 2); 137851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski i |= (i >> 4); 137951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski i |= (i >> 8); 138051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski i |= (i >> 16); 138151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return i - (i >>> 1); 138251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 138351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 138451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 138551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an {@code int} value with at most a single one-bit, in the 138651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * position of the lowest-order ("rightmost") one-bit in the specified 138751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code int} value. Returns zero if the specified value has no 138851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one-bits in its two's complement binary representation, that is, if it 138951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is equal to zero. 139051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 13916a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @param i the value whose lowest one bit is to be computed 139251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return an {@code int} value with a single one-bit, in the position 139351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the lowest-order one-bit in the specified value, or zero if 139451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified value is itself equal to zero. 139551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 139651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 139751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int lowestOneBit(int i) { 139851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // HD, Section 2-1 139951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return i & -i; 140051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 140151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 140251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 140351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the number of zero bits preceding the highest-order 140451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ("leftmost") one-bit in the two's complement binary representation 140551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the specified {@code int} value. Returns 32 if the 140651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified value has no one-bits in its two's complement representation, 140751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * in other words if it is equal to zero. 140851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 140951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Note that this method is closely related to the logarithm base 2. 141051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * For all positive {@code int} values x: 141151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 141251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>floor(log<sub>2</sub>(x)) = {@code 31 - numberOfLeadingZeros(x)} 141351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>ceil(log<sub>2</sub>(x)) = {@code 32 - numberOfLeadingZeros(x - 1)} 141451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 141551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 14166a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @param i the value whose number of leading zeros is to be computed 141751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the number of zero bits preceding the highest-order 141851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ("leftmost") one-bit in the two's complement binary representation 141951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the specified {@code int} value, or 32 if the value 142051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is equal to zero. 142151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 142251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 142351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int numberOfLeadingZeros(int i) { 142451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // HD, Figure 5-6 142551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (i == 0) 142651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return 32; 142751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int n = 1; 142851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (i >>> 16 == 0) { n += 16; i <<= 16; } 142951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (i >>> 24 == 0) { n += 8; i <<= 8; } 143051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (i >>> 28 == 0) { n += 4; i <<= 4; } 143151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (i >>> 30 == 0) { n += 2; i <<= 2; } 143251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski n -= i >>> 31; 143351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return n; 143451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 143551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 143651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 143751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the number of zero bits following the lowest-order ("rightmost") 143851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one-bit in the two's complement binary representation of the specified 143951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code int} value. Returns 32 if the specified value has no 144051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one-bits in its two's complement representation, in other words if it is 144151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * equal to zero. 144251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 14436a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @param i the value whose number of trailing zeros is to be computed 144451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the number of zero bits following the lowest-order ("rightmost") 144551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one-bit in the two's complement binary representation of the 144651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified {@code int} value, or 32 if the value is equal 144751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to zero. 144851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 144951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 145051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int numberOfTrailingZeros(int i) { 145151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // HD, Figure 5-14 145251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int y; 145351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (i == 0) return 32; 145451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int n = 31; 145551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski y = i <<16; if (y != 0) { n = n -16; i = y; } 145651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski y = i << 8; if (y != 0) { n = n - 8; i = y; } 145751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski y = i << 4; if (y != 0) { n = n - 4; i = y; } 145851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski y = i << 2; if (y != 0) { n = n - 2; i = y; } 145951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return n - ((i << 1) >>> 31); 146051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 146151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 146251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 146351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the number of one-bits in the two's complement binary 146451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * representation of the specified {@code int} value. This function is 146551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * sometimes referred to as the <i>population count</i>. 146651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 14676a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @param i the value whose bits are to be counted 146851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the number of one-bits in the two's complement binary 146951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * representation of the specified {@code int} value. 147051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 147151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 147251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int bitCount(int i) { 147351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // HD, Figure 5-2 147451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski i = i - ((i >>> 1) & 0x55555555); 147551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski i = (i & 0x33333333) + ((i >>> 2) & 0x33333333); 147651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski i = (i + (i >>> 4)) & 0x0f0f0f0f; 147751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski i = i + (i >>> 8); 147851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski i = i + (i >>> 16); 147951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return i & 0x3f; 148051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 148151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 148251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 148351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the value obtained by rotating the two's complement binary 148451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * representation of the specified {@code int} value left by the 148551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified number of bits. (Bits shifted out of the left hand, or 148651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * high-order, side reenter on the right, or low-order.) 148751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 148851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Note that left rotation with a negative distance is equivalent to 148951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * right rotation: {@code rotateLeft(val, -distance) == rotateRight(val, 149051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * distance)}. Note also that rotation by any multiple of 32 is a 149151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * no-op, so all but the last five bits of the rotation distance can be 149251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ignored, even if the distance is negative: {@code rotateLeft(val, 149351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * distance) == rotateLeft(val, distance & 0x1F)}. 149451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 14956a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @param i the value whose bits are to be rotated left 14966a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @param distance the number of bit positions to rotate left 149751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the value obtained by rotating the two's complement binary 149851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * representation of the specified {@code int} value left by the 149951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified number of bits. 150051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 150151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 150251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int rotateLeft(int i, int distance) { 150351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (i << distance) | (i >>> -distance); 150451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 150551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 150651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 150751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the value obtained by rotating the two's complement binary 150851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * representation of the specified {@code int} value right by the 150951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified number of bits. (Bits shifted out of the right hand, or 151051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * low-order, side reenter on the left, or high-order.) 151151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 151251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Note that right rotation with a negative distance is equivalent to 151351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * left rotation: {@code rotateRight(val, -distance) == rotateLeft(val, 151451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * distance)}. Note also that rotation by any multiple of 32 is a 151551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * no-op, so all but the last five bits of the rotation distance can be 151651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ignored, even if the distance is negative: {@code rotateRight(val, 151751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * distance) == rotateRight(val, distance & 0x1F)}. 151851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 15196a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @param i the value whose bits are to be rotated right 15206a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @param distance the number of bit positions to rotate right 152151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the value obtained by rotating the two's complement binary 152251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * representation of the specified {@code int} value right by the 152351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified number of bits. 152451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 152551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 152651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int rotateRight(int i, int distance) { 152751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (i >>> distance) | (i << -distance); 152851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 152951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 153051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 153151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the value obtained by reversing the order of the bits in the 153251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * two's complement binary representation of the specified {@code int} 153351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * value. 153451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 15356a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @param i the value to be reversed 153651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the value obtained by reversing order of the bits in the 153751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified {@code int} value. 153851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 153951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 154051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int reverse(int i) { 154151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // HD, Figure 7-1 154251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski i = (i & 0x55555555) << 1 | (i >>> 1) & 0x55555555; 154351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski i = (i & 0x33333333) << 2 | (i >>> 2) & 0x33333333; 154451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski i = (i & 0x0f0f0f0f) << 4 | (i >>> 4) & 0x0f0f0f0f; 154551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski i = (i << 24) | ((i & 0xff00) << 8) | 154651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski ((i >>> 8) & 0xff00) | (i >>> 24); 154751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return i; 154851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 154951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 155051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 155151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the signum function of the specified {@code int} value. (The 155251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * return value is -1 if the specified value is negative; 0 if the 155351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified value is zero; and 1 if the specified value is positive.) 155451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 15556a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @param i the value whose signum is to be computed 155651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the signum function of the specified {@code int} value. 155751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 155851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 155951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int signum(int i) { 156051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // HD, Section 2-7 156151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (i >> 31) | (-i >>> 31); 156251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 156351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 156451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 156551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the value obtained by reversing the order of the bytes in the 156651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * two's complement representation of the specified {@code int} value. 156751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 15686a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak * @param i the value whose bytes are to be reversed 156951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the value obtained by reversing the bytes in the specified 157051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code int} value. 157151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 157251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 157351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int reverseBytes(int i) { 157451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return ((i >>> 24) ) | 157551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski ((i >> 8) & 0xFF00) | 157651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski ((i << 8) & 0xFF0000) | 157751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski ((i << 24)); 157851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 157951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 15802729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath /** 15812729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * Adds two integers together as per the + operator. 15822729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * 15832729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * @param a the first operand 15842729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * @param b the second operand 15852729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * @return the sum of {@code a} and {@code b} 15862729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * @see java.util.function.BinaryOperator 15872729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * @since 1.8 15882729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath */ 15892729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath public static int sum(int a, int b) { 15902729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath return a + b; 15912729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath } 15922729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath 15932729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath /** 15942729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * Returns the greater of two {@code int} values 15952729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * as if by calling {@link Math#max(int, int) Math.max}. 15962729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * 15972729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * @param a the first operand 15982729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * @param b the second operand 15992729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * @return the greater of {@code a} and {@code b} 16002729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * @see java.util.function.BinaryOperator 16012729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * @since 1.8 16022729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath */ 16032729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath public static int max(int a, int b) { 16042729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath return Math.max(a, b); 16052729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath } 16062729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath 16072729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath /** 16082729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * Returns the smaller of two {@code int} values 16092729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * as if by calling {@link Math#min(int, int) Math.min}. 16102729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * 16112729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * @param a the first operand 16122729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * @param b the second operand 16132729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * @return the smaller of {@code a} and {@code b} 16142729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * @see java.util.function.BinaryOperator 16152729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath * @since 1.8 16162729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath */ 16172729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath public static int min(int a, int b) { 16182729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath return Math.min(a, b); 16192729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath } 16202729b8c4d261706b2ed7b17930d91c2b25e9a315Narayan Kamath 162151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** use serialVersionUID from JDK 1.0.2 for interoperability */ 16226a5351e12a69e865fe31b8aac989bd2573aebbcaPrzemyslaw Szczepaniak @Native private static final long serialVersionUID = 1360826667806852920L; 162351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski} 1624