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

总结

以上所述是小编给大家介绍的Java中Integer.valueOf,parsetInt() String.valueOf的区别和结果代码解析,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

你可能感兴趣的:(Java中Integer.valueOf,parsetInt() String.valueOf的区别和结果代码解析)