【JDK】:java.lang.Integer源碼解析

本文對JDK8中的java.lang.Integer包裝類的部分數值緩存技術、valueOf()、stringSize()、toString()、getChars()、parseInt()等進行簡要分析。

Integer緩存

先來看一段代碼:

Integer a1 = Integer.valueOf(13);
Integer a2 = Integer.valueOf(13);
Integer a3 = Integer.valueOf(133);
Integer a4 = Integer.valueOf(133);

System.out.println(a1 == a2);   // 輸出 true
System.out.println(a3 == a4);   // 輸出 false

兩個輸出語句具有不同的輸出,在於Integer使用了一個靜態內部類(嵌套類),裏面包含了一個緩存數組cache[],默認情況下,[-128, 127]之間的整數會在第一次使用時(類加載時)被自動裝箱,放在cache[]數組裏。區間的上限值high設置JVM參數-XX:AutoBoxCacheMax來改變,默認情況下參數爲127(byte類型的範圍),存儲在java.lang.Integer.IntegerCache.high屬性中。

    // 靜態內部類實現[-128, 127]的緩存
    private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];

        static {
            // high 值通過JVM進行設置,默認爲127
            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;

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

而使用Integer.valueOf()進行構造時,就使用了cache[]緩存數組。因此使用該方法構造的Integer對象如果在緩存區間內,會直接返回cache[]數組內的相應的引用,自然就是同一個對象;否則將生成一個全新的Integer對象。與此對應的,如果使用構造函數Integer()直接構造,根本沒有使用到緩存數組,生成的一定是全新的Integer對象。因此使用Integer.valueOf()構造能夠節省資源,提高效率。

    // 使用cache[]數組構造
    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

    // 使用構造函數構造
    public Integer(int value) {
        this.value = value;
    }

stringSize()

這個函數不是個public權限的函數,作爲內部工具方法使用。這個方法的實現是很巧妙的,避免除法、求餘等,判斷條件簡單,效率高(採用靜態field分析,而不是負責邏輯判斷可以明顯提高效果)。(int 最大長只有10)

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

    // Requires positive x 參數必須爲正數
    static int stringSize(int x) {
        for (int i=0; ; i++)
            if (x <= sizeTable[i])
                return i+1;
    }

toString(int i , int radix)

一個整數在給定進制的字符串表示。

public static String toString(int i, int radix) {
        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);
        int charPos = 32;  // int佔4個字節,32bit

        // 以負數爲基準進行處理
        if (!negative) {
            i = -i;
        }

        // 代碼的簡潔!! radix爲進制,最小爲2,最高位36
        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));
    }

上面的代碼使用了一個final static 的字符數組digits[],直接根據i與進制radix的求餘結果從digits[]裏面取值,提高運算效率。

    /** 所有可能代表數字的字符,最高支持36進制
     * 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'
    };

toString()

toString()方法返回當前Integer對象的字符串表示。可能有人覺得上面的toString(int i, int radix)已經是通用算法了,但是JDK在並沒有這樣(即radix是10的情況),而是採用了效率更高的方法。


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

    // toString()的調用方法
    // 必須先判斷Integer.MIN_VALUE,因爲getChars()方法中使用了i=-i
    // 以負數爲基準,對於i=Integer.MIN_VALUE將會產生溢出
    public static String toString(int i) {
        if (i == Integer.MIN_VALUE)
            return "-2147483648";
        // 獲取字符串表示的字符串長度,考慮了負數的符號位
        int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
        // 將Integer數讀入到char[]數組
        char[] buf = new char[size];
        getChars(i, size, buf);
        return new String(buf, true);
    }

這個算法的核心是getChars的實現,即將一個整數高效地逐位存入一個char數組中。

    // 核心代碼,從後向前將Integer讀入char[]字符表示數組,如果i = MIN_VALUE將會發生大數溢出
    // fail if i == Integer.MIN_VALUE
    static void getChars(int i, int index, char[] buf) {
        int q, r;
        int charPos = index;
        char sign = 0;

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

        // 處理超過2的16次方的大數
        // 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];  // 十位上的數字
        }

        // 處理小於2的16次方的數
        // Fall thru to fast mode for smaller numbers
        for (;;) {
            q = (i * 52429) >>> (16+3);  // 達到q=i/10的效果
            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 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',
        } ;

getChars()分別對int型的高位的兩個字節、低位的兩個字節進行遍歷。while部分的思想是,DigitOnes是代表個位,DigitTens代表十位,每次r可以迭代兩位(r就是除以100的餘數),每次找出兩位數,這樣有效的減少了乘除法的次數。至於移位運算,是爲了提高運算速度,q*100 = q*(2^6) +q*(2^5) + q*(2^2) = 64q+32q+4q.

for循環部分,q得到i截斷個位的值(q = i / 10 )。至於採用上述複雜的移位的目的是提高速度(>>>無符號右移)。q=i*(52429/216)/23≈≈i*0.1。因爲這裏要用i*52429>>>16更精確的表示乘以十分之八的作用,而高位的兩個字節的數再乘會溢出,所以源碼裏進行了高位與低位用兩種方式分開循環。

parseInt()

將String轉爲Int,相關的編程題參加劍指offer(56):表示數值的字符串

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

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

源碼中注意的幾點:

  • 所有的運算都是基於負數的。在toString也提到過,因爲將Integer.MIN_VALUE直接變換符號會導致數值溢出。
  • 溢出的判斷技巧。multmin = limit / radix 這個數的控制,可以在乘法計算之前可判斷計算之後是否溢出。同理,result < limit + digit 可在減法之前判斷計算後是否溢出。
發佈了117 篇原創文章 · 獲贊 184 · 訪問量 44萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章