Java中Integer.parseInt 和 Integer.valueOf 區別

Integer.parseInt() 和 Integer.valueOf() 都是用來 將String轉換爲Int的,但是爲什麼Java會提供兩個這樣的方法呢,他們如果是同樣的操作,豈不是多此一舉?

我們來深挖Java源代碼一探究竟。

  1. Integer.parseInt(),返回一個原子類型int.
  2. Integer.valueOf(), 返回的是封裝的Integer對象。

Integer.parseInt() 的源碼實現:

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

可以看到ParseInt() 只是調用parseInt, 並且返回原子類型int.
那麼valueOf 呢?

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

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

我們可以看到 valueOf 也會調用parseInt, 但是返回Integer對象。而且它會維護一個cache,如果int值在cache範圍內,直接從cache中取對象,如果不在,則會新創建一個對象。

所以我們可以得出結論,如果我們只是需要一個int值,parseInt是合適的,而且效率要高,但是如果用valueOf就多此一舉了,性能會下降。

同樣Integer, Long, Double, Float 都是一樣的道理。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章