The Integer method is Java’s encapsulation of int. It caches the Integer values from -128 to 127, including the conversion of different cardinality numbers, the method of converting each data type to the changed data type, and provides methods for comparison, summarization, and minimization.

// 非java属性,由c实现,最小值-2^31(-2147483648)
@Native public static final int   MIN_VALUE = 0x80000000;

// 非java属性,由c实现,最大值2^31-1(2147483647)
@Native public static final int   MAX_VALUE = 0x7fffffff;

// 是int的class实例,静态常量,代表Integer的基本数据类型
@SuppressWarnings("unchecked")
public static final Class<Integer>  TYPE = (Class<Integer>) Class.getPrimitiveClass("int");

// 静态常量数组,存储9个数字和26个小写字母,所有进制有可能用到表达数字的字符
final static char[] digits = {
    '0' , '1' , '2' , '3' , '4' , '5' ,
    '6' , '7' , '8' , '9' , 'a' , 'b' ,
    'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
    'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
    'o' , 'p' , 'q' , 'r' , 's' , 't' ,
    'u' , 'v' , 'w' , 'x' , 'y' , 'z'
};

// 参数(int整数,字符串中使用的基数)
public static String toString(int i, int radix) {
	// 基数范围,2和36之间,不在区间的默认10进制
    if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
        radix = 10;

	// 10进制直接转换
    if (radix == 10) {
        return toString(i);
    }
	
	// 存放转换进制后的字符数组
    char buf[] = new char[33];
	//判断是否为负数
    boolean negative = (i < 0);
	// 将指针指向buf数组最后一位,以便存放字符
    int charPos = 32;
	// 先转成负数处理,因为负数范围比整数大一
    if (!negative) {
        i = -i;
    }
	
	// 用短除法转换进制
    while (i <= -radix) {
		// 赋值每次的余数
        buf[charPos--] = digits[-(i % radix)];
        i = i / radix;
    }
	// 除的最终余数赋值在最前面
    buf[charPos] = digits[-i];

	// 存储负数位
    if (negative) {
        buf[--charPos] = '-';
    }

	// 返回字符串
    return new String(buf, charPos, (33 - charPos));
}

public static String toUnsignedString(int i, int radix) {
    return Long.toUnsignedString(toUnsignedLong(i), radix);
}

public static String toHexString(int i) {
    return toUnsignedString0(i, 4);
}

public static String toOctalString(int i) {
    return toUnsignedString0(i, 3);
}

public static String toBinaryString(int i) {
    return toUnsignedString0(i, 1);
}

private static String toUnsignedString0(int val, int shift) {
    // assert shift > 0 && shift <=5 : "Illegal shift value";
    int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val);
    int chars = Math.max(((mag + (shift - 1)) / shift), 1);
    char[] buf = new char[chars];

    formatUnsignedInt(val, shift, buf, 0, chars);

    // Use special constructor which takes over "buf".
    return new String(buf, true);
}

 static int formatUnsignedInt(int val, int shift, char[] buf, int offset, int len) {
    int charPos = len;
    int radix = 1 << shift;
    int mask = radix - 1;
    do {
        buf[offset + --charPos] = Integer.digits[val & mask];
        val >>>= shift;
    } while (val != 0 && charPos > 0);

    return charPos;
}

final static char [] DigitTens = {
    '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
    '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
    '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
    '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
    '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
    '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
    '6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
    '7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
    '8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
    '9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
    } ;

final static char [] DigitOnes = {
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    } ;

public static String toString(int i) {
    if (i == Integer.MIN_VALUE)
        return "-2147483648";
    int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
    char[] buf = new char[size];
    getChars(i, size, buf);
    return new String(buf, true);
}

public static String toUnsignedString(int i) {
    return Long.toString(toUnsignedLong(i));
}

static void getChars(int i, int index, char[] buf) {
    int q, r;
    int charPos = index;
    char sign = 0;

    if (i < 0) {
        sign = '-';
        i = -i;
    }

    // Generate two digits per iteration
    while (i >= 65536) {
        q = i / 100;
    // really: r = i - (q * 100);
        r = i - ((q << 6) + (q << 5) + (q << 2));
        i = q;
        buf [--charPos] = DigitOnes[r];
        buf [--charPos] = DigitTens[r];
    }

    // Fall thru to fast mode for smaller numbers
    // assert(i <= 65536, i);
    for (;;) {
        q = (i * 52429) >>> (16+3);
        r = i - ((q << 3) + (q << 1));  // r = i-(q*10) ...
        buf [--charPos] = digits [r];
        i = q;
        if (i == 0) break;
    }
    if (sign != 0) {
        buf [--charPos] = sign;
    }
}

// 用于判断多少位的数组列表
final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
                                  99999999, 999999999, Integer.MAX_VALUE };

// 将数字进行判断,最后得出多少位进行加1
static int stringSize(int x) {
    for (int i=0; ; i++)
        if (x <= sizeTable[i])
            return i+1;
}

// 传参字符串数字,基数,将字符串转换为Integer
public static int parseInt(String s, int radix)
            throws NumberFormatException
{
    /*
     * WARNING: This method may be invoked early during VM initialization
     * before IntegerCache is initialized. Care must be taken to not use
     * the valueOf method.
     */
	// 抛出异常
    if (s == null) {
        throw new NumberFormatException("null");
    }
	// 最小二进制
    if (radix < Character.MIN_RADIX) {
        throw new NumberFormatException("radix " + radix +
                                        " less than Character.MIN_RADIX");
    }
	// 最大36进制
    if (radix > Character.MAX_RADIX) {
        throw new NumberFormatException("radix " + radix +
                                        " greater than Character.MAX_RADIX");
    }
	// 返回结果
    int result = 0;
	// 正负数标识
    boolean negative = false;
	// 字符串数字长度
    int i = 0, len = s.length();
	
	// 定义比较的初始化值,用作判断
    int limit = -Integer.MAX_VALUE;
    int multmin;
    int digit;
	
	// 字符串数字长度大于0
    if (len > 0) {
		// 获取字符串第一个为符号
        char firstChar = s.charAt(0);
		// 判断是否是是非法字符
        if (firstChar < '0') { // Possible leading "+" or "-"
			// 为"-"号,标记符号,重新定义比较值
            if (firstChar == '-') {
                negative = true;
                limit = Integer.MIN_VALUE;
            } else if (firstChar != '+')
				// 不等于“+-”的非法字符直接抛出异常
                throw NumberFormatException.forInputString(s);
			
			// 只有非法字符也抛出异常
            if (len == 1) // Cannot have lone "+" or "-"
                throw NumberFormatException.forInputString(s);
            i++;
        }
		
		// 将最小值往右一位
        multmin = limit / radix;
		// 循环索引
        while (i < len) {
            // Accumulating negatively avoids surprises near MAX_VALUE
			// 把字符转换成对应基数的数值
            digit = Character.digit(s.charAt(i++),radix);
			// 字符如果小于0,代表是非法字符
            if (digit < 0) {
                throw NumberFormatException.forInputString(s);
            }
			// 不能小于multmin,因为下面*进制就会超出范围了
            if (result < multmin) {
                throw NumberFormatException.forInputString(s);
            }
			// 左移一位,0的话直接为0
            result *= radix;
			// 如果值小于最小值加字符,防止下面减去超出范围
            if (result < limit + digit) {
				// 抛出非法字符异常
                throw NumberFormatException.forInputString(s);
            }
			// 将上面的字符放进来
            result -= digit;
        }
    } else {
        throw NumberFormatException.forInputString(s);
    }
	// 授予数字的符号,计算的时候正数变成负数,负数变成正数
    return negative ? result : -result;
}

// 默认采用10进制处理
public static int parseInt(String s) throws NumberFormatException {
    return parseInt(s,10);
}

public static int parseUnsignedInt(String s, int radix)
            throws NumberFormatException {
    if (s == null)  {
        throw new NumberFormatException("null");
    }

    int len = s.length();
    if (len > 0) {
        char firstChar = s.charAt(0);
        if (firstChar == '-') {
            throw new
                NumberFormatException(String.format("Illegal leading minus sign " +
                                                   "on unsigned string %s.", s));
        } else {
            if (len <= 5 || // Integer.MAX_VALUE in Character.MAX_RADIX is 6 digits
                (radix == 10 && len <= 9) ) { // Integer.MAX_VALUE in base 10 is 10 digits
                return parseInt(s, radix);
            } else {
                long ell = Long.parseLong(s, radix);
                if ((ell & 0xffff_ffff_0000_0000L) == 0) {
                    return (int) ell;
                } else {
                    throw new
                        NumberFormatException(String.format("String value %s exceeds " +
                                                            "range of unsigned int.", s));
                }
            }
        }
    } else {
        throw NumberFormatException.forInputString(s);
    }
}

public static int parseUnsignedInt(String s) throws NumberFormatException {
    return parseUnsignedInt(s, 10);
}

// 有问题抛出数字格式异常,传参字符串类型数字和基数
public static Integer valueOf(String s, int radix) throws NumberFormatException {
    return Integer.valueOf(parseInt(s,radix));
}

// 有问题抛出数字格式异常,传参字符串类型数字,默认按10进制
public static Integer valueOf(String s) throws NumberFormatException {
    return Integer.valueOf(parseInt(s, 10));
}

// Integer缓存类,缓存了-128到127的Integer值,遇到范围内的直接取值
private static class IntegerCache {
    static final int low = -128;
    static final int high;
    static final Integer cache[];
	
	// 静态代码块
    static {
        // hige默认是127,但可以通过jvm的一个参数扩展高值 java -Djava.lang.Integer.IntegerCache.high = xxxx Aclass.class
        int h = 127;
        String integerCacheHighPropValue =
            sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
        if (integerCacheHighPropValue != null) {
            try {
                int i = parseInt(integerCacheHighPropValue);
                i = Math.max(i, 127);
                // Maximum array size is Integer.MAX_VALUE
                h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
            } catch( NumberFormatException nfe) {
                // If the property cannot be parsed into an int, ignore it.
            }
        }
        high = h;
		
		// 定义cache数组
        cache = new Integer[(high - low) + 1];
        int j = low;
        for(int k = 0; k < cache.length; k++)
			// 直接给该范围内的数字对象化,所以该范围取的是一个对象,地址相同
            cache[k] = new Integer(j++);
		
		// 断言,如果high<127会出错终止程序运行并打印出来
        // range [-128, 127] must be interned (JLS7 5.1.7)
        assert IntegerCache.high >= 127;
    }

    private IntegerCache() {}
}

// 首先判断在不在缓存范围,在的话从缓存中取,不在的话创建一个新对象。
public static Integer valueOf(int i) {
    if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}

private final int value;

// int构造函数
public Integer(int value) {
    this.value = value;
}

// String构造函数
public Integer(String s) throws NumberFormatException {
    this.value = parseInt(s, 10);
}

// 强制转换
public byte byteValue() {
    return (byte)value;
}

// 强制转换
public short shortValue() {
    return (short)value;
}

// 返回int值
public int intValue() {
    return value;
}

// 强制转换
public long longValue() {
    return (long)value;
}

// 强制转换
public float floatValue() {
    return (float)value;
}

// 强制转换
public double doubleValue() {
    return (double)value;
}

// 转String
public String toString() {
    return toString(value);
}

@Override
public int hashCode() {
    return Integer.hashCode(value);
}

// 重写hashcode
public static int hashCode(int value) {
    return value;
}

// 比较相等方法
public boolean equals(Object obj) {
    if (obj instanceof Integer) {
        return value == ((Integer)obj).intValue();
    }
    return false;
}

// 传参nm返回指定名称的系统属性值
public static Integer getInteger(String nm) {
    return getInteger(nm, null);
}

// 传参nm返回指定名称的系统属性值,如果为空,要初始化val为对象返回
public static Integer getInteger(String nm, int val) {
    Integer result = getInteger(nm, null);
    return (result == null) ? Integer.valueOf(val) : result;
}

// 根据nm返回指定名称的系统属性值,本方法是用来获取系统参数值
// java.version  Java 运行时环境版本
// java.vendor  Java 运行时环境供应商
// java.vendor.url  Java 供应商的 URL
// java.home  Java 安装目录
public static Integer getInteger(String nm, Integer val) {
    String v = null;
    try {
        v = System.getProperty(nm);
    } catch (IllegalArgumentException | NullPointerException e) {
    }
    if (v != null) {
        try {
            return Integer.decode(v);
        } catch (NumberFormatException e) {
        }
    }
    return val;
}

// 对数字进行解码(2、8、16进制)
public static Integer decode(String nm) throws NumberFormatException {
    int radix = 10;
    int index = 0;
    boolean negative = false;
    Integer result;

    if (nm.length() == 0)
        throw new NumberFormatException("Zero length string");
    char firstChar = nm.charAt(0);
    // Handle sign, if present
    if (firstChar == '-') {
        negative = true;
        index++;
    } else if (firstChar == '+')
        index++;

    // Handle radix specifier, if present
    if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
        index += 2;
        radix = 16;
    }
    else if (nm.startsWith("#", index)) {
        index ++;
        radix = 16;
    }
    else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
        index ++;
        radix = 8;
    }

    if (nm.startsWith("-", index) || nm.startsWith("+", index))
        throw new NumberFormatException("Sign character in wrong position");

    try {
        result = Integer.valueOf(nm.substring(index), radix);
        result = negative ? Integer.valueOf(-result.intValue()) : result;
    } catch (NumberFormatException e) {
        // If number is Integer.MIN_VALUE, we'll end up here. The next line
        // handles this case, and causes any genuine format error to be
        // rethrown.
        String constant = negative ? ("-" + nm.substring(index))
                                   : nm.substring(index);
        result = Integer.valueOf(constant, radix);
    }
    return result;
}

// 比较方法
public int compareTo(Integer anotherInteger) {
    return compare(this.value, anotherInteger.value);
}

// 比较方法
public static int compare(int x, int y) {
    return (x < y) ? -1 : ((x == y) ? 0 : 1);
}

public static int compareUnsigned(int x, int y) {
    return compare(x + MIN_VALUE, y + MIN_VALUE);
}

public static long toUnsignedLong(int x) {
    return ((long) x) & 0xffffffffL;
}

public static int divideUnsigned(int dividend, int divisor) {
    // In lieu of tricky code, for now just use long arithmetic.
    return (int)(toUnsignedLong(dividend) / toUnsignedLong(divisor));
}

public static int remainderUnsigned(int dividend, int divisor) {
    // In lieu of tricky code, for now just use long arithmetic.
    return (int)(toUnsignedLong(dividend) % toUnsignedLong(divisor));
}

@Native public static final int SIZE = 32;

public static final int BYTES = SIZE / Byte.SIZE;

public static int highestOneBit(int i) {
    // HD, Figure 3-1
    i |= (i >>  1);
    i |= (i >>  2);
    i |= (i >>  4);
    i |= (i >>  8);
    i |= (i >> 16);
    return i - (i >>> 1);
}

public static int lowestOneBit(int i) {
    // HD, Section 2-1
    return i & -i;
}

public static int numberOfLeadingZeros(int i) {
    // HD, Figure 5-6
    if (i == 0)
        return 32;
    int n = 1;
    if (i >>> 16 == 0) { n += 16; i <<= 16; }
    if (i >>> 24 == 0) { n +=  8; i <<=  8; }
    if (i >>> 28 == 0) { n +=  4; i <<=  4; }
    if (i >>> 30 == 0) { n +=  2; i <<=  2; }
    n -= i >>> 31;
    return n;
}

public static int numberOfTrailingZeros(int i) {
    // HD, Figure 5-14
    int y;
    if (i == 0) return 32;
    int n = 31;
    y = i <<16; if (y != 0) { n = n -16; i = y; }
    y = i << 8; if (y != 0) { n = n - 8; i = y; }
    y = i << 4; if (y != 0) { n = n - 4; i = y; }
    y = i << 2; if (y != 0) { n = n - 2; i = y; }
    return n - ((i << 1) >>> 31);
}

public static int bitCount(int i) {
    // HD, Figure 5-2
    i = i - ((i >>> 1) & 0x55555555);
    i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
    i = (i + (i >>> 4)) & 0x0f0f0f0f;
    i = i + (i >>> 8);
    i = i + (i >>> 16);
    return i & 0x3f;
}

public static int rotateLeft(int i, int distance) {
    return (i << distance) | (i >>> -distance);
}

public static int rotateRight(int i, int distance) {
    return (i >>> distance) | (i << -distance);
}

public static int reverse(int i) {
    // HD, Figure 7-1
    i = (i & 0x55555555) << 1 | (i >>> 1) & 0x55555555;
    i = (i & 0x33333333) << 2 | (i >>> 2) & 0x33333333;
    i = (i & 0x0f0f0f0f) << 4 | (i >>> 4) & 0x0f0f0f0f;
    i = (i << 24) | ((i & 0xff00) << 8) |
        ((i >>> 8) & 0xff00) | (i >>> 24);
    return i;
}

public static int signum(int i) {
    // HD, Section 2-7
    return (i >> 31) | (-i >>> 31);
}

public static int reverseBytes(int i) {
    return ((i >>> 24)           ) |
           ((i >>   8) &   0xFF00) |
           ((i <<   8) & 0xFF0000) |
           ((i << 24));
}

// 汇总
public static int sum(int a, int b) {
    return a + b;
}

// 取最大值
public static int max(int a, int b) {
    return Math.max(a, b);
}

// 取最小值
public static int min(int a, int b) {
    return Math.min(a, b);
}

// 序列号id
@Native private static final long serialVersionUID = 1360826667806852920L;
Copy the code