Java interview must-see Integer.parseInt () and Integer.valueOf ()

Integer.parseInt () and Integer.valueOf () will be the String are converted to Int, but why Java will provide two such way to do that, if they are the same operation, is it unnecessary?

Let's root out the Java source code to find out.

Integer.parseInt (), returns an atomic type int.
Integer.valueOf (), returns the package Integer object.

We look Integer.parseInt () source code to achieve:

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

 

 

You can see ParseInt () simply calls parseInt, and return atomic type int.
So valueOf it?

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

 

 

We can see valueOf can also call parseInt, then return Integer object. And it will maintain a cache, the cache if int value in the range of objects taken directly from the cache, if not, you can create a new object.

Therefore, we can convert, if we just need an int value, parseInt is appropriate, and the efficiency is higher, but if the value of it superfluous, performance degrades.

Similarly Integer, Long, Double, Float is the same reason.


Author: Xiaoqiang chat architecture
link: https: //www.imooc.com/article/37688 block_id = tuijian_wz?

 

Guess you like

Origin www.cnblogs.com/hxun/p/11686543.html