Java源码解析,Integer

源码分析,基本上都加载注解上了,如有谬误,请指正,谢谢。
jdk1.8.0_161

public class Integer {
    /**
     * 最小值,-2的31次方
     */
    @Native
    public static final int MIN_VALUE = 0x80000000;

    /**
     * 最大值2的31次方减1
     */
    @Native
    public static final int MAX_VALUE = 0x7fffffff;

    /**
     * int类型的class对象
     */
    @SuppressWarnings("unchecked")
    public static final Class TYPE = (Class) Class.getPrimitiveClass("int");

    /**
     * All possible chars for representing a number as a String
     */
    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的字符串形式,这个基数可以是2到36之间(包括2和36)的任意数
     * ,传入的可以是十六进制,8进制,10进制
     * -5会返回-101
     */
    public static String toString(int i, int radix) {
        // 如果超出了基数范围,那么就按照10进制处理
        if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
            radix = 10;

        // 如果是10进制的话,直接用toString(i)比较快
        if (radix == 10) {
            return toString(i);
        }

        char buf[] = new char[33];
        boolean negative = (i < 0);
        int charPos = 32;

        // 如果是正数先给转成负数,这里不是很明白为啥都要用负数
        if (!negative) {
            i = -i;
        }

        // 这里循环整除,顺带查了下 a,b均为整数时: a%b=a-(a/b)*b; a,b中有浮点数: a%b=a-(b*q),这里q=int(a/b)
        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));
    }

    /**
     * 根据基数,返回指定数的无符号字符串,也就是-5返回11111111111111111111111111111011,5返回101
     */
    public static String toUnsignedString(int i, int radix) {
        return Long.toUnsignedString(toUnsignedLong(i), radix);
    }

    /**
     * 16进制,负数返回的是无符号的数
     */
    public static String toHexString(int i) {
        return toUnsignedString0(i, 4);
    }

    /**
     * 8进制,负数返回的是无符号的数
     */
    public static String toOctalString(int i) {
        return toUnsignedString0(i, 3);
    }

    /**
     * 2进制,负数返回的是无符号的数
     */
    public static String toBinaryString(int i) {
        return toUnsignedString0(i, 1);
    }

    /**
     * 将证书根据偏移量shift转为无符号的数。
     * shift=1时,就是2进制
     * shift=2时,就是4进制
     * shift=3时,就是8进制
     * shift=4时,就是16进制
     * shift=5时,就是32进制
     * 因为int类型最多站32字节,所以这个偏移量最多5
     */
    private static String toUnsignedString0(int val, int shift) {
        // 算出数字占的2进制中的位数
        int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val);
        // 计算出占用的char的长度,mag是二进制的数字实际占的位数,shift是偏移量
        // TODO 这一段不知道为什么要这么算,感觉上可以用mag%shift == 0 ? mag / shift : mag / shift + 1更好理解
        // TODO 而源码这样写是有什么我未考虑到的,或者效率,或者其他原因吗?
        // TODO 不过源码这样做感觉更加简洁一些
        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);
    }

    /**
     * 格式化一个int类型的数到char数组,返回数组长度
     */
    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;
    }

    /**
     * 这就是一个10*10的枚举,枚举的是一个数十位上的值,
     * 比如,0->9的时候,他的十位就是0,11->19的十位就是1.
     */
    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',
    };

    /**
     * 这就是一个10*10的枚举,枚举的是一个数个位上的值,
     */
    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',
    };

    // I use the "invariant division by multiplication" trick to
    // accelerate Integer.toString.  In particular we want to
    // avoid division by 10.
    //
    // The "trick" has roughly the same performance characteristics
    // as the "classic" Integer.toString code on a non-JIT VM.
    // The trick avoids .rem and .div calls but has a longer code
    // path and is thus dominated by dispatch overhead.  In the
    // JIT case the dispatch overhead doesn't exist and the
    // "trick" is considerably faster than the classic code.
    //
    // TODO-FIXME: convert (x * 52429) into the equiv shift-add
    // sequence.
    //
    // RE:  Division by Invariant Integers using Multiplication
    //      T Gralund, P Montgomery
    //      ACM PLDI 1994
    //

    /**
     * 返回数值的十进制表示形式的字符串
     */
    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];
        // buf中填充值
        getChars(i, size, buf);
        return new String(buf, true);
    }

    /**
     * 返回指定参数的无符号的十进制形式,如5,返回的是5,-5返回4294967291
     */
    public static String toUnsignedString(int i) {
        return Long.toString(toUnsignedLong(i));
    }

    /**
     * 将给定的数,转成他的十进制char数组。
     */
    static void getChars(int i, int index, char[] buf) {
        int q, r;
        int charPos = index;
        char sign = 0;

        // 负数转为正数
        if (i < 0) {
            sign = '-';
            i = -i;
        }

        // 大于65536(2的16次方,short最大值)时,进入这里,每次循环生成2个数字
        while (i >= 65536) {
            q = i / 100;
            // 刚开始看这个计算感觉一脸懵逼,然后将2的6次方、2的5次方、2的2次方加起来后,就等于100
            // 然后开始看两个DigitOnes、DigitTens数组,也感觉很懵逼,结果就是枚举了下100以内的所有情况
            // really: r = i - (q * 100);
            r = i - ((q << 6) + (q << 5) + (q << 2));
            i = q;
            buf[--charPos] = DigitOnes[r];
            buf[--charPos] = DigitTens[r];
        }

        // 较小的数字用快速模式,这里为什么要把这个叫做快速模式呢
        // TODO 这里是纯粹的主观观点,如有误和不同观点,欢迎提出讨论 start
        // 个人考虑,首先因为接下来的数据可能是个1位数,
        // 直接除以100在取十位和个位有问题,所以要加判断。那么还不如把这两个分开。
        // 那么为什么是以65535分割呢,这个希望有哪位可以解惑
        // 当指定的数据类型范围在short内直接进入这里
        // TODO 这里是纯粹的主观观点,如有误和不同观点,欢迎提出讨论 end
        // assert(i <= 65536, i);
        for (; ; ) {
            // 这里应该是要q = q / 10,但是这个算法是真的牛逼呀,效率体现在细节上
            // 这里做了下预算2的19次方等于524288,那么也就是将整除的运算转成了位移运算
            // 对比 q = (i * 52429) >>> (16 + 3); 和 q = q / 10;的效率
            // 个人做了下测试10万次循环内,前者平均13毫秒,后者平均24毫秒,效率差距已经出来了
            // 在测试100万和1000万时出现了不稳定现象,单总体上还是前者快于后者
            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;
        }
    }

    /**
     * 这个数组主要用于计算数值的长度,当数值小于数组下标的某个值,那么数值的长度就是下标+1,不过需要的是正整数
     */
    final static int[] sizeTable = {9, 99, 999, 9999, 99999, 999999, 9999999,
            99999999, 999999999, Integer.MAX_VALUE};

    /**
     * 计算数值的长度,不过需要的是正整数
     * @param x
     * @return
     */
    // Requires positive x
    static int stringSize(int x) {
        for (int i = 0; ; i++)
            if (x <= sizeTable[i])
                return i + 1;
    }

    /**
     * 根据基数,返回字符串代表的数字的十进制形式
     * parseInt("0", 10) returns 0
     * parseInt("473", 10) returns 473
     * parseInt("+42", 10) returns 42
     * parseInt("-0", 10) returns 0
     * parseInt("-FF", 16) returns -255
     * parseInt("1100110", 2) returns 102
     * parseInt("2147483647", 10) returns 2147483647
     * parseInt("-2147483648", 10) returns -2147483648
     * parseInt("2147483648", 10) throws a NumberFormatException
     * parseInt("99", 8) throws a NumberFormatException
     * parseInt("Kona", 10) throws a NumberFormatException
     * parseInt("Kona", 27) returns 411787
     * parseInt("-101", 2) returns -5
     */
    public static int parseInt(String s, int radix)
            throws NumberFormatException {
        /*
         * 这个方法在VM初始化IntegerCache完成之前可能会被调用,所以必须注意不要使用valueOf这个方法。
         * 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");
        }

        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
            char firstChar = s.charAt(0);
            // 判断是否是小于字符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) {
                digit = Character.digit(s.charAt(i++), radix);
                if (digit < 0) {
                    throw NumberFormatException.forInputString(s);
                }
                if (result < multmin) {
                    throw NumberFormatException.forInputString(s);
                }
                // 累加结果,主要就是这一步和下面的result -= digit;
                result *= radix;
                if (result < limit + digit) {
                    throw NumberFormatException.forInputString(s);
                }
                result -= digit;
            }
        } else {
            throw NumberFormatException.forInputString(s);
        }
        return negative ? result : -result;
    }

    /**
     * 将字符串参数解析为带符号的十进制整数
     */
    public static int parseInt(String s) throws NumberFormatException {
        return parseInt(s, 10);
    }

    /**
     * 与toUnsignedString对应,根据基数将无符号的字符串表示的数还原为十进制
     */
    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 {
                // Integer的最大值,在基数最大也就是36的时候,也才6位数。
                // 当进制是10进制的时候,转成的字符串长度是10
                if (len <= 5 ||
                        (radix == 10 && len <= 9)) {
                    return parseInt(s, radix);
                } else {
                    // 其他的无符号位的数由long统一解决,这个方法在Long中进行注解
                    long ell = Long.parseLong(s, radix);
                    // 算出来的这个数必须在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);
        }
    }

    /**
     * 与toUnsignedString对应,根据基数10将无符号的字符串表示的数还原为十进制
     */
    public static int parseUnsignedInt(String s) throws NumberFormatException {
        return parseUnsignedInt(s, 10);
    }

    /**
     * 根据指定基数,返回字符串的十进制的Integer对象
     */
    public static Integer valueOf(String s, int radix) throws NumberFormatException {
        return Integer.valueOf(parseInt(s, radix));
    }

    /**
     * 基数为10,返回字符串的十进制的Integer对象
     */
    public static Integer valueOf(String s) throws NumberFormatException {
        return Integer.valueOf(parseInt(s, 10));
    }

    /**
     * Cache to support the object identity semantics of autoboxing for values between
     * -128 and 127 (inclusive) as required by JLS.
     * 

* The cache is initialized on first usage. The size of the cache * may be controlled by the {@code -XX:AutoBoxCacheMax=} option. * During VM initialization, Integer.IntegerCache.high property * may be set and saved in the private system properties in the * sun.misc.VM class. */ /** * Integer类的缓存,缓存了-128到127.缓存存入了常量池中 */ private static class IntegerCache { static final int low = -128; static final int high; static final Integer cache[]; static { // 缓存最大值,可以被调整,所以这里获取下,两者取最大 int h = 127; String integerCacheHighPropValue = sun.misc.VM.getSavedProperty("Integer.IntegerCache.high"); if (integerCacheHighPropValue != null) { try { int i = parseInt(integerCacheHighPropValue); i = Math.max(i, 127); // 如果设置的缓存最大值是int的最大值,那么这里就需要做一个处理,必须缓存-128和0 h = Math.min(i, Integer.MAX_VALUE - (-low) - 1); } catch (NumberFormatException nfe) { // 如果缓存设置的值,不能转换横int,这里会忽略,也可以认为缓存的最大值就是127了 // If the property cannot be parsed into an int, ignore it. } } high = h; cache = new Integer[(high - low) + 1]; int j = low; for (int k = 0; k < cache.length; k++) cache[k] = new Integer(j++); // 缓存的范围必须包含[-128, 127] assert Integer.IntegerCache.high >= 127; } private IntegerCache() { } } /** * 如果指定的数据是在缓存内,直接从缓存中取,如果不在缓存内,那么new一个新的Integer * 而Integer的自动装箱就是用的这个方法,而不是new */ public static Integer valueOf(int i) { if (i >= Integer.IntegerCache.low && i <= Integer.IntegerCache.high) return Integer.IntegerCache.cache[i + (-Integer.IntegerCache.low)]; return new Integer(i); } /** * Integer存的具体的int值,final类型 * * @serial */ private final int value; /** * 实例化,每次实例化都是一个新的对象 */ public Integer(int value) { this.value = value; } /** * 实例化,传入的字符串必须是int类型的十进制的 */ public Integer(String s) throws NumberFormatException { this.value = parseInt(s, 10); } /** * 缩窄到byte,如果value超出了byte的范围,那么会返回截取后的数,不会报错的 * 这种截位是直接截取的后8位, * 如:Integer a = 128;a.byteValue = -128。而-128的二进制表达式是 * 11111111111111111111111110000000 */ public byte byteValue() { return (byte) value; } /** * 缩窄到short,如果value超出了short的范围,那么会返回截取后的数,不会报错的 * 这种截位是直接截取的后8位, */ public short shortValue() { return (short) value; } /** * 返回int值 */ public int intValue() { return value; } /** * 返回long,向上转型 */ public long longValue() { return (long) value; } /** * 返回float,向上转型 */ public float floatValue() { return (float) value; } /** * 返回double,向上转型 */ public double doubleValue() { return (double) value; } /** * 返回十进制表达形式的字符串 */ public String toString() { return toString(value); } /** * hashCode */ @Override public int hashCode() { return Integer.hashCode(value); } /** * hashCode就是int本身 */ public static int hashCode(int value) { return value; } /** * equals */ public boolean equals(Object obj) { if (obj instanceof Integer) { return value == ((Integer) obj).intValue(); } return false; } /** * 根据系统属性名返回对应的Integer对象 */ public static Integer getInteger(String nm) { return getInteger(nm, null); } /** * 根据系统属性名返回对应的Integer对象,有默认值 */ public static Integer getInteger(String nm, int val) { Integer result = getInteger(nm, null); return (result == null) ? Integer.valueOf(val) : result; } /** * 根据系统属性名返回对应的Integer对象,有默认值 */ 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; } /** * 解码字符串,返回一个Integer * 可带-、+号 * 可接受十进制、16进制、8进制 * #、0x和0X开头代表16进制 * 0开头代表8进制 *

*
*
DecodableString: *
Signopt DecimalNumeral *
Signopt {@code 0x} HexDigits *
Signopt {@code 0X} HexDigits *
Signopt {@code #} HexDigits *
Signopt {@code 0} OctalDigits *

*/ 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); // 判断正负 if (firstChar == '-') { negative = true; index++; } else if (firstChar == '+') index++; // 判断基数,及索引开始位置 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) { // 当传入的数字是Integer.MIN_VALUE,需要在这里单独处理下 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); } /** * 将指定值转换成一个无符号位的long,转换之后高32位是0,低32位代表数值 */ 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)); } // Bit twiddling /** * int所占的bit */ @Native public static final int SIZE = 32; /** * int所占的byte */ public static final int BYTES = SIZE / Byte.SIZE; /** * 二进制最高位1的权值, * 如5的二进制101,最低位的1在最低位,权值为4 * 10的二进制1010,最低位的1在倒数第二位,权值为8 */ 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); } /** * 二进制最低位1的权值, * 如5的二进制101,最低位的1在最低位,权值为1 * 10的二进制1010,最低位的1在倒数第二位,权值为2 */ public static int lowestOneBit(int i) { // HD, Section 2-1 return i & -i; } /** * 计算数字的二进制高位上的0的个数 */ 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; } /** * 计算数字的二进制低位上的0的个数 * 比如8的二进制1000,返回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); } /** * 计算指定数的二进制码中为1的个数 */ 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; } /** * 循环左移,也就是将i向左移动distance位, * 当i的移位到达了最高位还没有够distance,那么移位的数就去低位开头,继续移位。 * 所以叫做循环移位。distance可以为负 */ public static int rotateLeft(int i, int distance) { return (i << distance) | (i >>> -distance); } /** * 循环右移,也就是将i向右移动distance位, * 当i的移位到达了最低位还没有够distance,那么移位的数就去高位开头,继续移位。 * 所以叫做循环移位。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; } /** * 判断传入的值是正数、负数或者零 * -1,负数 * 1,正数 * 0,零 */ public static int signum(int i) { // 这算法,惊为天人呀 return (i >> 31) | (-i >>> 31); } /** * 按照一个Byte位,也就是8位翻转指定值 */ 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); } /** * 序列化 */ @Native private static final long serialVersionUID = 1360826667806852920L; }

你可能感兴趣的:(Java源码解析,Integer)