开源中文网

您的位置: 首页 > Java语言 > 正文

JAVA中Integer.valueOf, parsetInt() String.valueOf的区别和结果

来源: 网络整理  作者: 佚名

先来看段代码






public class IntegerDemo {



    public static void main(String[] args) {

        String num = null;

        System.out.println( Integer.parseInt(num));// Exception java.lang.NumberFormatException

        System.out.println( Integer.valueOf(num));// Exception java.lang.NumberFormatException

        System.out.println( String.valueOf(num)); //输出null

       

        num = "";

        System.out.println( Integer.parseInt(num)); // Exception java.lang.NumberFormatException

        System.out.println( Integer.valueOf(num)); // Exception java.lang.NumberFormatException

        System.out.println( String.valueOf(num));//空串,啥也不输出

    }

}


先看一下 String.valueOf() 里面是怎么写的



String.valueOf() 在遇到 null 和 空串的情况下 ,都能正常输出,所以不抛错



再来看下 包装类型 Integer里面又是如何处理的





这两个方法里面都需要先 parseInt( s,10),就是将字符串s先转成  十进制的 int基本类型,,但是 valueOf()会根据int范围从[-127,127]的内部缓存中去取(用到设计模式中的 享元模式)

一起来看下 parseInt(s, 10),,在方法里面会判断字符串是否是合法的数字,会去校验null, 空串等其他格式,所以会抛错


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

Tags:结果
关于开源中文网 - 联系我们 - 广告服务 - 网站地图 - 版权声明