美文网首页
Integer类源码笔记

Integer类源码笔记

作者: LuckyBuzz | 来源:发表于2020-05-04 15:41 被阅读0次

    Integer类将基础类型int包装到了对象中,形成一个引用类型。它提供的主要功能是与int相关的类型封装与转换。

    int与Integer之间的转换也可以通过自动装箱与拆箱进行。自动装箱时,编译器调用valueOf将原始类型值转换成对象;自动拆箱时,编译器通过调用类似intValue() ,doubleValue()这类的方法将对象转换成原始类型值。
    https://www.jianshu.com/p/0ce2279c5691

    Integer类继承了Number类,而Number类是一个抽象类,也是所有可以转换成基础类型的数值型类的父类。他的主要作用是对数值型数据类型转换进行规范。

    public final class Integer extends Number implements Comparable<Integer>
    

    所在路径:\java\lang\Integer.java

    Integer类的成员变量 Integer类方法列表1 Integer类方法列表2

    其中,Number类源码为:

    public abstract class Number implements java.io.Serializable { 
        public abstract int intValue(); 
        public abstract long longValue(); 
        public abstract float floatValue(); 
        public abstract double doubleValue(); 
        public byte byteValue() { 
            return (byte)intValue(); 
        } 
          public short shortValue() { 
            return (short)intValue(); 
        } 
        private static final long serialVersionUID = -8742448824652078965L; 
    }
    

    一、成员变量

    首先是常规成员变量,包括序列化id,value和size等,其中BYTES属性表示一个Integer对象的值需要用4字节来表示(1字节的长度是8位二进制)。

    /** serialVersionUID */
    @Native private static final long serialVersionUID = 1360826667806852920L;
    /** 存储对象值 */
    private final int value;
    /** 存储对象长度 */
    @Native public static final int SIZE = 32;
    /** 计算出表示一个Integer对象的值需要多少字节 */
    public static final int BYTES = SIZE / Byte.SIZE;
    

    @Native:表明一个字段引用的值可能来自于本地代码,不影响java本身代码逻辑,这是一个Java8后新增的元注解。与native关键字不同,@Native注解主要用来修饰Filed。

    这里面有一个问题:String类的UID是没有被@Native修饰的,而Integer类却被修饰了,这里的原因我还不是很清楚。不过根据注释的描述,这个注解的主要作用应该是hint,所以不会影响代码的实际逻辑。

    Java中int类型是32位的、有符号的以二进制补码表示的整数。最小值是-2,147,483,648(-2^31),最大值是 2,147,483,647(2^31 - 1)。

    /** 最小值-2的31次方 */
    @Native public static final int   MIN_VALUE = 0x80000000;
    /** 最小值-2的31次方减1 */
    @Native public static final int   MAX_VALUE = 0x7fffffff;
    
    /** TYPE是一个表示原始类型int的变量。在调用Integer.TYPE时,程序返回的是int;而在调用int.class时,程序返回的是Integer对象。*/
    @SuppressWarnings("unchecked")
    public static final Class<Integer>  TYPE = (Class<Integer>) Class.getPrimitiveClass("int");
    

    @SuppressWarnings:指示编译器忽略注解中声明的警告。

    其他成员变量主要用来辅助计算一些常用值,比如输入int类型的位数,数字字符集合等。

    /** 用来确定入参的位数,详见stringSize()方法 */
    final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
                                      99999999, 999999999, Integer.MAX_VALUE };
    
    /** 因为进制转换最多支持到36进制,所以可能出现的char一共有36种 */
    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'
    };
    
    /** 十位数字字符表 */
    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',
        } ;
    

    二、构造器

    Integer只有两个构造器,一个是直接输入int类型参数,另一个是将String类型参数转换成int型(value属性是int型的)。

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

    其中,parseInt方法也是Integer类自身提供的方法,它的第一个入参表示将要被转换的字符串,第二个入参表示进制,更多细节将在下文进行详细介绍。

    三、parseInt()和parseUnsignedInt()

    Integer类提供了将字符串转换成有符号整形和无符号整形的方法。

    1、parseInt()

    如果不传入转换进制这个参数,则默认按10进制进行转换。

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

    转换时,Integer实际上使用了Character.digit()方法。这个方法会根据输入的字符和进制调用CharacterData.of().digit()方法,其本质上是进行了移位和强制类型转换,将单个字符处理成数字。数字返回后,Integer.parseInt()方法会进行拼接处理。

    public static int parseInt(String s, int radix)
                throws NumberFormatException {
        if (s == null) {
            throw new NumberFormatException("null");
        }
    
        // 支持的进制转换范围是2-36
        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) {
                // Accumulating negatively avoids surprises near MAX_VALUE
                digit = Character.digit(s.charAt(i++),radix);
                if (digit < 0) {
                    throw NumberFormatException.forInputString(s);
                }
                if (result < multmin) {
                    throw NumberFormatException.forInputString(s);
                }
                result *= radix;
                if (result < limit + digit) {
                    throw NumberFormatException.forInputString(s);
                }
                result -= digit;
            }
        } else {
            throw NumberFormatException.forInputString(s);
        }
        return negative ? result : -result;
    }
    
    2、parseUnsignedInt()

    parseUnsignedInt()方法实际上就是不考虑有符号的情况,如果有负号则抛错,否则直接调用parseInt()方法或者parseLong()方法。

    public static int parseUnsignedInt(String s) throws NumberFormatException {
        return parseUnsignedInt(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);
        }
    }
    

    四、toString()

    1、默认十进制的转换

    和String类一样,Integer类也重写了toString()方法。当入参为int类型时,程序调用stringSize()方法计算要返回的字符串数组的长度。

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

    其中stringSize()方法采用查表的方式确定位数,默认入参为正,且位数以十进制计。

    final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
                                      99999999, 999999999, Integer.MAX_VALUE };
    
    static int stringSize(int x) {
        for (int i=0; ; i++)
            if (x <= sizeTable[i])
                return i+1;
    }
    

    getChars()方法实际是哪个也是通过查表给一个空的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;
        }
    
        // 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;
        }
    }
    

    转换的最后采用return new String(buf, true);的方式而不是直接使用String类型的其他构造器,这在一定程度上避免了Arrays.copoyOf(),提高了赋值效率。

    2、支持其他进制的转换
    public static String toString(int i, int radix) {
        if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
            radix = 10;
    
        /* Use the faster version */
        if (radix == 10) {
            return toString(i);
        }
    
        char buf[] = new char[33];
        boolean negative = (i < 0);
        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));
    }
    

    此外,Integer类还提供了toUnsignedString(),toBinaryString(),toOctalString(),toHexString()等方法。

    五、IntegerCache和valueOf()

    IntegerCache是Integer类的一个内部类,它的作用是在常量池中预先存入默认-128至127的int类型整数。当Integer类的其他方法用到cache中的整数时,可以直接从常量池中读取,而不是重新创建一个。这样做的结果是:两个从缓存中取出的int型整数会被==判定成相等的,即便他们使用了不同Integer对象的valueOf()方法。

    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);
                    // 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 = 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() {}
    }
    

    valueOf()方法是对IntegerCache的直接应用,如果在范围内则从缓存中获取,否则重新创建一个对象。

    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
    

    同样,Integer类还提供了一些返回其他类型的valueOf()方法。

    public byte byteValue() {
        return (byte)value;
    }
    
    public short shortValue() {
        return (short)value;
    }
    
    public int intValue() {
        return value;
    }
    
    public long longValue() {
        return (long)value;
    }
    
    public float floatValue() {
        return (float)value;
    }
    
    public double doubleValue() {
        return (double)value;
    }
    

    六、equals()和hashcode()

    Integer类重写了equals()和hashcode()方法,其中equals()方法只在输入类型是Integer时才调用Integer.intValue()方法进行比较,比较的双方是两个int类型变量。否则直接返回false。如果入参obj是一个int类型变量,程序会进行自动装箱。

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

    重写后的hashcode()方法实际上就是把value当做hashcode返回了,这样一来,两个Integer对象只要做到value相等就会拥有相同的hashcode。

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

    七、getInteger()和decode()

    getInteger()是用来通过System.getProperty()获得参数,并将其解析成Integer类型的方法。Integer类重载的三个getInteger()最终都是调用了下面这段代码。

    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()方法用来解析获取到的字符串,他的作用是确定进制并调用Integer.valueOf()进行数字解析。

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

    八、常用工具方法

    Integer类还封装了一些常用的工具方法,包括比较大小,取最大最小值等。

    1、compareTo()
    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);
    }
    

    无符号数的表会调用compareUnsigned()方法。

    public static int compareUnsigned(int x, int y) {
        return compare(x + MIN_VALUE, y + MIN_VALUE);
    }
    
    2、位处理

    下面两个方法分别返回的是一个int型变量最高位和最低位的权值,而不是最高位和最低位本身。

    https://blog.csdn.net/Jiapengcs/article/details/73321638

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

    计算机以补码表示一个负数,所以对入参取负再进行位操作时,实际上操作的是他的补码。

    <<:表示带符号左移,相当于乘2,低位移出(舍弃),高位的空位补符号位,即正数补零,负数补1。符度号位不变。
    >>:表示带符号右移,相当于除2
    >>>:表示无符号右移,移动得到的空位以零填问充

    bitCount()方法用于统计二进制中1的个数。

    https://www.cnblogs.com/inmoonlight/p/9301733.html

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

    rotate()方法实现了循环左移和循环右移。

    https://blog.csdn.net/zuoyouzouzou/article/details/88892198

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

    reverse()方法实现了bit位的翻转,reverseBytes()则是按byte翻转。

    https://blog.csdn.net/u013190513/article/details/70593257

    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 reverseBytes(int i) {
        return ((i >>> 24)           ) |
               ((i >>   8) &   0xFF00) |
               ((i <<   8) & 0xFF0000) |
               ((i << 24));
    }
    

    signum()方法可以返回入参的符号函数(如果指定值为负,则返回-1;如果指定值为零,则返回0;如果指定的值为正,则返回1)。

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

    numberOfLeadingZeros()和numberOfTrailingZeros()分别可以获得入参转换成二进制后,bit位为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;
    }
    
    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);
    }
    
    3、数学计算
    /** 求和 */
    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);
    }
    
    /** 无符号除 */
    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));
    }
    

    相关文章

      网友评论

          本文标题:Integer类源码笔记

          本文链接:https://www.haomeiwen.com/subject/vaqyghtx.html