Integer 类继承 Number 类,实现 Comparable 接口。Integer 类被修饰为 final,所以其不可被继承。

# 字段

# MIN_VALUE

最小值,@Native 表示该字段可以从本地代码引用。

@Native public static final int MIN_VALUE = 0x80000000;

# MAX_VALUE

最大值。

@Native public static final int MAX_VALUE = 0x7fffffff;

# TYPE

类原始类型 int 的类实例。@SuppressWarnings ("unchecked") 表示该字段应该抑制 unchecked 的编译器警告。

@SuppressWarnings("unchecked")
public static final Class<Integer>  TYPE = (Class<Integer>) Class.getPrimitiveClass("int");

# digits

所有可以表示为字符的数字。此变量是默认修饰符 ,只能在包内访问。

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'
	};

# DigitTens

处理大的数,代表十位。

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

# DigitOnes

处理大的数,代表个位。

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

# sizeTable

为了得到十进制的位数。

final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
                                      99999999, 999999999, Integer.MAX_VALUE };

# value

Integer 的值,value 被定义为 final,即常量,值不可更改。

private final int value;

# SIZE

用于表示二进制补码二进制形式的 int 值的位数。

@Native public static final int SIZE = 32;

# BYTES

用于表示二进制补码二进制形式的 int 值的字节数。

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

# serialVersionUID

Integer 的序列化版本号。

@Native private static final long serialVersionUID = 1360826667806852920L;

# 构造方法

# Integer(int value)

public Integer(int value) {
    this.value = value;
}

# Integer(String s)

public Integer(String s) throws NumberFormatException {
    this.value = parseInt(s, 10);
}

# 方法

# toString(int i, int radix)

i 是要转换为字符串的整数,radix 是在字符串表示中使用的基数,返回基数 radix 表示的 i 的字符串。

public static String toString(int i, int radix) {
    // 如果 radix 不在合法的基数范围之内,使用 10 作为基数,范围 2~36。
    if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
        radix = 10;
    // 如果基数是 10,使用更加快速的转换方法。
    if (radix == 10) {
        return toString(i);
    }
    // Integer 转化为以 2 为基数的字符串最多需要 32 位,并且有可能需要一位来表示 '-' 字符。
    char buf[] = new char[33];
    boolean negative =(i < 0);
    //buf [] 下标。
    int charPos = 32;
    // 如果 i 是非负数,将 i 变为负数。
    if (!negative) {
        i = -i;
    }
    // 计算 i 转换为以 radix 为基数的值,从后向前遍历 buf [],并放入 bug 中。
    // 转化为负数来操作是因为正数取不到 128。
    while (i <= -radix) {
        buf[charPos--] = digits[-(i % radix)];
        i = i / radix;
    }
    buf[charPos] = digits[-i];
    // 如果是负数,加上字符 '-'。
    if (negative) {
        buf[--charPos] = '-';
    }
   	// 返回 buf [] 中有值的部分的字符串。
    return new String(buf, charPos, (33 - charPos));
}

# toUnsignedString(int i, int radix)

i 是要转换为字符串的整数,radix 是在字符串表示中使用的基数,返回无符号整数 i 以基数 radix 表示的字符串。具体参考 Long 的源码阅读。

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

# toHexString(int i)

i 是要转换为字符串的整数,返回无符号整数 i 以基数 16 表示的字符串。

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

# toOctalString(int i)

i 是要转换为字符串的整数,返回无符号整数 i 以基数 8 表示的字符串。

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

# toBinaryString(int i)

i 是要转换为字符串的整数,返回无符号整数 i 以基数 2 表示的字符串。

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

# toUnsignedString0(int val, int shift)

将 val 转换为以 shift 表示的基数的字符串,val 是要转换的整数,shift 是log2xlog_{2}x 的值,x 是进制数。

private static String toUnsignedString0(int val, int shift) {
    // assert shift > 0 && shift <=5 : "Illegal shift value";
    // 得到 val 的二进制的有意义的位数,例如 126 对应的 msg 是 7,-126 对应的 mag 是 32。
    int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val);
    //msg 位数转换为 2^shift 进制的位数
    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);
}

# formatUnsignedInt(int val, int shift, char[] buf, int offset, int len)

val 是要格式化的整数,shift 是要格式化的基数的代表,4 代表十六进制,3 代表八进制,1 代表二进制,buf 是要写入的字符数组,offset 是 buf 中开始的位置,len 是要写入 buf 的长度,返回 buf 中最低位的位置。

static int formatUnsignedInt(int val, int shift, char[] buf, int offset, int len) {
    int charPos = len;
    int radix = 1 << shift;
    int mask = radix - 1;
    // 此过程相当于从右向左遍历 val 的二进制,每 shift 位转换为十进制对应 radix 进制的一位数。
    // 就如同二进制转换为十六进制时,每四位换算成十进制就是十六进制的一位;二进制转换为八进制时,每三位换算成十进制就是八进制的一位。
    do {
        buf[offset + --charPos] = Integer.digits[val & mask];
        val >>>= shift;
    } while (val != 0 && charPos > 0);
    return charPos;
}

# toString(int i)

将 i 转换为 string 类型,返回 String 类型的 i。

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);
}

# toUnsignedString(int i)

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

# getChars(int i, int index, char[] buf)

将整数 i 一个字符一个字符地写入 buf,index 是 buf 的起始位置。

static void getChars(int i, int index, char[] buf) {
    int q, r;
    //buf 的下标
    int charPos = index;
    // 正负数标志
    char sign = 0;
    // 如果是负数,将 sign 赋值为 '-'
    if (i < 0) {
        sign = '-';
        i = -i;
    }
    // Generate two digits per iteration
    // 以中间位数为界限,大的数使用一种算法,小的数使用一种算法
    // 2^16=65536
    while (i >= 65536) {
        q = i / 100;
        // really: r = i - (q * 100);
        // (q << 6) + (q << 5) + (q << 2) = q * (2^6 + 2^5 + 2^2) = 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 (;;) {
        // 为什么不使用 i/10 呢???因为效率方面乘法优于除法,位运算优于乘法
        // 2^19=524288
        // 2^15<52429<2^16
        // 为什么是 16+3 呢???为了精度
        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;
    }
}

# stringSize(int x)

获取整型 x 转为 string 之后的字符串长度。

static int stringSize(int x) {
    for (int i=0; ; i++)
        if (x <= sizeTable[i])
            return i+1;
}

# parseInt(String s, int radix)

将字符串 s 以 radix 为基数转换为有符号整型数。如果发生以下任何一种情况,则抛出类型 NumberFormatException 的异常:

  • 第一个参数是 null 或是长度为零的字符串。
  • 基数小于 Character.MIN_RADIX 或大于 Character.MAX_RADIX
  • 字符串的任何字符不是指定基数的数字,除了第一个字符可能是减号或加号,前提是字符串长度大于 1.
  • 由字符串表示的值不是 int 类型的值。
public static int parseInt(String s, int radix) throws NumberFormatException {
	if (s == null) {
        throw new NumberFormatException("null");
    }
    if (radix < Character.MIN_RADIX) {
        throw new NumberFormatException("radix " + radix + " less than Character.MIN_RADIX");
    }
    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;
    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) {
            // 积极消极避免 MAX_VALUE 附近的意外
            // 获取以 radix 为基数的字符的十进制表示,Character.digit ('f', 16) = 15
            digit = Character.digit(s.charAt(i++),radix);
            if (digit < 0) {
                throw NumberFormatException.forInputString(s);
            }
            // 避免 result*radix 数值超过整型范围
            if (result < multmin) {
                throw NumberFormatException.forInputString(s);
            }
            // 进位
            result *= radix;
            // 避免 result-digit 数值超过整型范围
            if (result < limit + digit) {
                throw NumberFormatException.forInputString(s);
            }
            result -= digit;
        }
    } else {
        throw NumberFormatException.forInputString(s);
    }
    return negative ? result : -result;
}

# parseInt(String s)

将字符串 s 以 10 为基数转换为有符号整型数。

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

# parseUnsignedInt(String s, int radix)

将字符串 s 以 radix 为基数转换为无符号整型数。如果发生以下任何一种情况,则抛出类型 NumberFormatException 的异常:

  • 第一个参数是 null 或是长度为零的字符串。
  • 基数小于 Character.MIN_RADIX 或大于 Character.MAX_RADIX
  • 字符串的任何字符不是指定基数的数字,除了第一个字符可能是加号,前提是字符串长度大于 1.
  • 由字符串表示的值大于最大的无符号整数值23112^{31}-1
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 {
            // 此处有一个分界线,若以最大基数即 36 转换字符串,则该字符串长度不能超过 5,否则会超过 int 的取值范围
            // 至于为什么十进制特殊了一下,可能是十进制比较常用吧,不然使用 Long 处理会占用空间吧。
            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);
                // 判断 ell 是否超出 int 的取值范围
                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);
    }
}

# parseUnsignedInt(String s)

将字符串 s 以 10 为基数转换为无符号整型数。

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

# Integer valueOf(String s, int radix)

以 radix 为基数将字符串 s 转换为 Integer 类型。返回的 Integer 类型的数优先从缓存中获取。

public static Integer valueOf(String s, int radix) throws NumberFormatException {
    return Integer.valueOf(parseInt(s,radix));
}

# valueOf(String s)

以 10 为基数将字符串 s 转换为 Integer 类型。返回的 Integer 类型的数优先从缓存中获取。

public static Integer valueOf(String s) throws NumberFormatException {
    return Integer.valueOf(parseInt(s, 10));
}

# valueOf(int i)

将整型数 i 转换为 Integer 类型数。返回的 Integer 类型的数优先从缓存中获取。

public static Integer valueOf(int i) {
    // 先从缓存中获取
    if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}

# byteValue()

返回 byte 类型的值。

public byte byteValue() {
    return (byte)value;
}

# shortValue()

返回 short 类型的值。

public short shortValue() {
    return (short)value;
}

# intValue()

返回 int 类型的值。

public int intValue() {
    return value;
}

# longValue()

返回 long 类型的值。

public long longValue() {
    return (long)value;
}

# floatValue()

返回 long 类型的值。

public float floatValue() {
    return (float)value;
}

# doubleValue()

返回 double 类型的值。

public double doubleValue() {
    return (double)value;
}

# toString()

返回 String 类型。

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

# hashCode()

返回哈希值。由此可见,Integer 对象的 hashcode 值是它的值。

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

# hashCode(int value)

返回 value 的哈希值。

public static int hashCode(int value) {
    return value;
}

# equals(Object obj)

判断 obj 的 value 与调用该方法的对象的 value 是否相等。

public boolean equals(Object obj) {
    if (obj instanceof Integer) {
        return value == ((Integer)obj).intValue();
    }
    return false;
}

# getInteger(String nm)

返回系统属性 nm 的整数值。

public static Integer getInteger(String nm) {
    return getInteger(nm, null);
}

# getInteger(String nm, int val)

返回系统属性 nm 的整数值,如果为空,返回默认的 val 的 Integer。

public static Integer getInteger(String nm, int val) {
    Integer result = getInteger(nm, null);
    return (result == null) ? Integer.valueOf(val) : result;
}

# getInteger(String nm, Integer val)

返回系统属性 nm 的整数值,如果为空,返回默认的 val。

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;
}

# decode(String nm)

将 nm 解码为 Integer。

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;
    }
    // 如果 0 后面没有字符了,则 nm 就只是 0 而已,不是八进制。
    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) {
        // 如果 number 是 Integer.MIN_VALUE,由于正数最大值是 Integer.MIN_VALUE-1,nm.substring (index) 作为 int 类型的参数传入会有错误。 
        // 将 nm.substring (index) 转为负数来处理。
        String constant = negative ? ("-" + nm.substring(index))
            : nm.substring(index);
        result = Integer.valueOf(constant, radix);
    }
    return result;
}

# compareTo(Integer anotherInteger)

调用该方法的对象的 value 与参数中的对象的 value 相比较,this.value 比 anotherInteger.value 小返回 - 1,this.value 比 anotherInteger.value 大返回 1,this.value 等于 anotherInteger.value 返回 0。

public int compareTo(Integer anotherInteger) {
    return compare(this.value, anotherInteger.value);
}

# compare(int x, int y)

比较 x 和 y 的大小,x 比 y 小返回 - 1,x 比 y 大返回 1,x 等于 y 返回 0。

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

# compareUnsigned(int x, int y)

将 x,y 视为无符号比较大小,因为负数符号位为 1,所以正数一定比负数小。

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

# toUnsignedLong(int x)

将 x 视为无符号转为 long 型。

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

# divideUnsigned(int dividend, int divisor)

将 dividend 和 divisor 视为无符号相除。

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));
}

# remainderUnsigned(int dividend, int divisor)

忽略符号取 dividend 除以 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));
}

# highestOneBit(int i)

返回十进制数转为二进制最高位表示的十进制的数值。

public static int highestOneBit(int i) {
    // HD, Figure 3-1
    // 使得 i 的二进制从排除符号位的最高位到最低位数值均为 1
    i |= (i >>  1);
    i |= (i >>  2);
    i |= (i >>  4);
    i |= (i >>  8);
    i |= (i >> 16);
    return i - (i >>> 1);
}

# lowestOneBit(int i)

返回十进制数转为二进制最低位表示的十进制的数值,即忽略符号位之后最高位的数值。

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

# numberOfLeadingZeros(int i)

i 是整数,返回 i 的补码中最高位 0 的位数,从左向右数,下一位不是 0 的位数,范围是 0~32,0 代表没有。此方法很机智地采用了二分法的思想。numberOfLeadingZeros (8)=28。

public static int numberOfLeadingZeros(int i) {
    // HD, Figure 5-6
    if (i == 0)
        return 32;
    int n = 1;
    // 前 16 位上都是 0,左移 16 位
    if (i >>> 16 == 0) { n += 16; i <<= 16; }
    // 前 8 位都是 0,左移 8 位
    if (i >>> 24 == 0) { n +=  8; i <<=  8; }
    // 前 4 位都是 0,左移 4 位
    if (i >>> 28 == 0) { n +=  4; i <<=  4; }
    // 前 2 位都是 0,左移 2 位
    if (i >>> 30 == 0) { n +=  2; i <<=  2; }
    // 减去第 1 位上数值
    n -= i >>> 31;
    return n;
}

# numberOfTrailingZeros(int i)

i 是整数,返回 i 的补码中最低位 0 的位数。从右向左数,下一位不是 0 的位数。numberOfLeadingZeros (8)=3。

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);
}

# bitCount(int i)

返回 i 的补码中值为 1 的位数的个数。

public static int bitCount(int i) {
    // 每两位存储每两位值为 1 的个数。
    i = i - ((i >>> 1) & 0x55555555);
    // 每四位存储每四位值为 1 的个数。
    i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
    // 每八位存储每八位值为 1 的个数。
    i = (i + (i >>> 4)) & 0x0f0f0f0f;
    // 每十六位存储每十六位值为 1 的个数。
    i = i + (i >>> 8);
    // 每三十二位存储每三十二位值为 1 的个数。
    i = i + (i >>> 16);
    // 由于值最大为 32,所以只关注后六位即可。
    return i & 0x3f;
}

# rotateLeft(int i, int distance)

i 的二进制值循环左移 distance 位。普通左移会丢失最左边位数的数,但是循环左移最左边的数会循环到最右边。

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

# rotateRight(int i, int distance)

i 的二进制值循环右移 distance 位。

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

# reverse(int i)

反转 i 的二进制值上每一位的数。

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=ABCD
    // i << 24    D000
    // (i & 0xff00) << 8    0C00
    // (i >>> 8) & 0xff00    00B0
    // i >>> 24    000A
    i = (i << 24) | ((i & 0xff00) << 8) |
        ((i >>> 8) & 0xff00) | (i >>> 24);
    return i;
}

# signum(int i)

如果 i 为负数返回 - 1,如果 i 为正数返回 1,如果 i 为 0 返回 0。

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

# reverseBytes(int i)

反转 i 的二进制值的每一个字节上数。

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

# sum(int a, int b)

返回 a 和 b 的和。

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

# max(int a, int b)

返回 a,b 中的最大值。

public static int max(int a, int b) {
    return Math.max(a, b);
}

# min(int a, int b)

返回 a,b 中的最小值。

public static int min(int a, int b) {
    return Math.min(a, b);
}

# 内部类

# IntegerCache

此内部类用来缓存部分大小的 Integer,默认范围 - 128-127。

private static class IntegerCache {
    static final int low = -128;
    static final int high;
    static final Integer cache[];
    static {
        // high value may be configured by property
        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);
                // 确保数组的大小最大是 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;
        // 缓存从 low 到 high 的整数
        cache = new Integer[(high - low) + 1];
        int j = low;
        for(int k = 0; k < cache.length; k++)
            cache[k] = new Integer(j++);
        // range [-128, 127] must be interned (JLS7 5.1.7)
        assert IntegerCache.high >= 127;
    }
    private IntegerCache() {}
}