Integer的parseInt和valueOf的區別

先來看一下下面這段代碼

String s = "1";
System.out.println(Integer.valueOf(s));
System.out.println(Integer.parseInt(s));

輸出結果是什麼?沒錯,一樣都是1。兩個方法都可以把數字類型字符串轉成int類型整數,但是這兩個方法還是有一點區別的,valueOf(String s)方法調用了parseInt(String s, int radix)方法,而parseInt(String s, int radix)方法返回值是一個int類型的值,之後又調用了valueOf(int i)方法將int進行了裝箱返回包裝類型Integer。

所以如果你不需要返回包裝類型,可以直接調用parseInt(String s)方法,效率更高。

下面這段是valueOf(String s)方法的源碼

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

可以看到調用parseInt方法的時候還傳了一個int類型參數radix,這個參數表示進制,默認使用十進制進行轉換。下面是方法的源碼,我標註了一些註釋。

 public static int parseInt(String s, int radix)
            throws NumberFormatException
    {

        /* 警告:在初始化IntegerCache之前,VM初始化期間可能會提前調用此方法。 必須注意不要使用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.
         */

        //字符串爲空則拋出NumberFormatException
        if (s == null) {
            throw new NumberFormatException("null");
        }
        //傳的進制參數小於2,拋出NumberFormatException,並且提示進制小於最小進制
        if (radix < Character.MIN_RADIX) {
            throw new NumberFormatException("radix " + radix +
                    " less than Character.MIN_RADIX");
        }
        //傳的進制參數小於36,拋出NumberFormatException,並且提示進制小於最大進制
        if (radix > Character.MAX_RADIX) {
            throw new NumberFormatException("radix " + radix +
                    " greater than Character.MAX_RADIX");
        }

        int result = 0;
        //negative 用來判斷結果是否爲負數
        boolean negative = false;
        //獲取字符串長度
        int i = 0, len = s.length();
        //limit = -2147483647
        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 != '+')
                    //不爲負數或正數,拋出NumberFormatException
                    throw NumberFormatException.forInputString(s);

                //長度爲1,拋出NumberFormatException
                if (len == 1) // Cannot have lone "+" or "-"
                    throw NumberFormatException.forInputString(s);
                i++;
            }
            //計算 multmin ,注意負數和正數的limit是不一樣的,負數的limit = -2147483648,正數的limit = -2147483647
            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 {
            //字符不爲空,但是字符長度等於0,拋出NumberFormatException
            throw NumberFormatException.forInputString(s);
        }
        //根據正負數的標識來判斷結果取正還是取反
        return negative ? result : -result;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章