zoukankan      html  css  js  c++  java
  • 深挖的Java源代码之Integer.parseInt()vs Integer.valueOf()

    Integer.parseInt()和Integer.valueOf()都是用来将String转换为Int的,但是为什么Java会提供两个这样的方法呢,他们如果是同样的操作,岂不是多此一举?

    我们来深挖的Java源代码一探究竟。

    的Integer.parseInt()返回一个原子类型INT。
    Integer.valueOf(),返回的是封装的整数对象。

    我们来看一下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对象。而且它会维维护一个缓存,如果int值在缓存范围内,直接从缓存中取对象,如果不在,则会新创建一个对象。

    所以我们可以得出结论,如果我们只是需要一个INT值,parseInt函数是合适的,而且效率要高,但是如果用的valueOf就多此一举了,性能会下降。

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

    链接:https://www.imooc.com/article/37688?block_id=tuijian_wz

  • 相关阅读:
    34、JS/AJAX
    33、mybatis(二)
    32、mybatis
    31、springmvc(注解)
    30、springmvc
    29、Oralce(五)
    Spring学习之路-SpringBoot简单入门
    Spring学习之路-从放弃到入门
    心情日记
    Spring学习之路-从入门到放弃
  • 原文地址:https://www.cnblogs.com/hxun/p/11375302.html
Copyright © 2011-2022 走看看