关于Integer.parseInt()方法的使用

解析Integer.parseInt()方法

我看到这个知识点是Java面试基础中的考点,所以自己为了以后面试打算自己过一遍。

我看到别人博客上对源码直接是文字说明,我觉得效果不是很好,我这里直接代数测试这个源码运行流程。

 

1.我带入一个正正整数 256 注意看注释中的数值


 
  1.      public static int parseInt(String s, int radix)
  2.  
  3.      {
  4.          /*
  5.              * WARNING: This method may be invoked early during VM initialization
  6.              * before IntegerCache is initialized. Care must be taken to not use
  7.              * the valueOf method.
  8.              */
  9.  
  10.          //判断基数是否在 2~36之间
  11.          if (s == null) {
  12.              throw new NumberFormatException("null");
  13.          }
  14.  
  15.          if (radix < Character.MIN_RADIX) {
  16.              throw new NumberFormatException("radix " + radix +
  17.                      " less than Character.MIN_RADIX");
  18.          }
  19.  
  20.          if (radix > Character.MAX_RADIX) {
  21.              throw new NumberFormatException("radix " + radix +
  22.                      " greater than Character.MAX_RADIX");
  23.          }
  24.  
  25.          int result = 0;
  26.          boolean negative = false;
  27.          int i = 0, len = s.length();
  28.          int limit = -Integer.MAX_VALUE; //-2147483647
  29.          int multmin;
  30.          int digit;
  31.          //字符串中的是否有符号
  32.          if (len > 0) {
  33.              char firstChar = s.charAt(0);
  34.              if (firstChar < '0') { // Possible leading "+" or "-"
  35.                  if (firstChar == '-') {
  36.                      negative = true;
  37.                      limit = Integer.MIN_VALUE;
  38.                  } else if (firstChar != '+')
  39.                      throw NumberFormatException.forInputString(s);
  40.  
  41.                  if (len == 1) // Cannot have lone "+" or "-"
  42.                      throw NumberFormatException.forInputString(s);
  43.                  i++;
  44.              }
  45.              //计算multmin 值
  46.              multmin = limit / radix;
  47.              // multmin = -214748364
  48.  
  49.              //开始循环
  50.              while (i < len) {
  51.                  // Accumulating negatively avoids surprises near MAX_VALUE
  52.                  //获取字符转换成对应进制的整数
  53.                  digit = Character.digit(s.charAt(i++),radix);
  54.                  //第一次循环 digit = 2;
  55.                  //第二次循环 digit = 5;
  56.                  //第三次循环 digit = 6;
  57.                  if (digit < 0) {
  58.                      throw NumberFormatException.forInputString(s);
  59.                  }
  60.                  if (result < multmin) {
  61.                      throw NumberFormatException.forInputString(s);
  62.                  }
  63.                  result *= radix;
  64.                  //第一次循环 result = 0;
  65.                  //第二次循环 result = -20;
  66.                  //第三次循环 result = -250;
  67.                  if (result < limit + digit) {
  68.                  //第一次循环 limit + digit = -2147483645;
  69.                  //第二次循环 limit + digit = -2147483640;
  70.                  //第三次循环 limit + digit = -2147483634;
  71.  
  72.                      throw NumberFormatException.forInputString(s);
  73.                  }
  74.                  result -= digit;
  75.                  //第一次循环 result = -2;
  76.                  //第二次循环 result = -25;
  77.                  //第三次循环 result = -256;
  78.  
  79.              }
  80.          } else {
  81.              throw NumberFormatException.forInputString(s);
  82.          }
  83.          return negative ? result : -result;
  84.          //negative 值为false,所以 -result = -(-256) = 256 返回结果
  85.      }

 

2.我再带入一个负数 -256


 
  1.      public static int parseInt(String s, int radix)
  2.  
  3.      {
  4.          /*
  5.              * WARNING: This method may be invoked early during VM initialization
  6.              * before IntegerCache is initialized. Care must be taken to not use
  7.              * the valueOf method.
  8.              */
  9.  
  10.          //判断基数是否在 2~36之间
  11.          if (s == null) {
  12.              throw new NumberFormatException("null");
  13.          }
  14.  
  15.          if (radix < Character.MIN_RADIX) {
  16.              throw new NumberFormatException("radix " + radix +
  17.                      " less than Character.MIN_RADIX");
  18.          }
  19.  
  20.          if (radix > Character.MAX_RADIX) {
  21.              throw new NumberFormatException("radix " + radix +
  22.                      " greater than Character.MAX_RADIX");
  23.          }
  24.  
  25.          int result = 0;
  26.          boolean negative = false;
  27.          int i = 0, len = s.length();
  28.          int limit = -Integer.MAX_VALUE; //-2147483647
  29.          int multmin;
  30.          int digit;
  31.          //字符串中的是否有符号
  32.          if (len > 0) {
  33.              char firstChar = s.charAt(0);
  34.              if (firstChar < '0') { // Possible leading "+" or "-"
  35.                  if (firstChar == '-') {
  36.                  //走这里
  37.                      negative = true;
  38.                      limit = Integer.MIN_VALUE;
  39.                  //此时 limit = -2147483648;
  40.                  } else if (firstChar != '+')
  41.                      throw NumberFormatException.forInputString(s);
  42.  
  43.                  if (len == 1) // Cannot have lone "+" or "-"
  44.                      throw NumberFormatException.forInputString(s);
  45.                  i++;
  46.              }
  47.              //计算multmin 值
  48.      multmin = limit / radix;
  49.              // multmin = -214748364
  50.  
  51.              //开始循环
  52.              while (i < len) {
  53.                  // Accumulating negatively avoids surprises near MAX_VALUE
  54.                  //获取字符转换成对应进制的整数
  55.                  digit = Character.digit(s.charAt(i++),radix);
  56.                  //第一次循环 digit = 2;
  57.                  //第二次循环 digit = 5;
  58.                  //第三次循环 digit = 6;
  59.                  if (digit < 0) {
  60.                      throw NumberFormatException.forInputString(s);
  61.                  }
  62.                  if (result < multmin) {
  63.                      throw NumberFormatException.forInputString(s);
  64.                  }
  65.                  result *= radix;
  66.                  //第一次循环 result = 0;
  67.                  //第二次循环 result = -20;
  68.                  //第三次循环 result = -250;
  69.                  if (result < limit + digit) {
  70.                  //第一次循环 limit + digit = -2147483646;
  71.                  //第二次循环 limit + digit = -2147483641;
  72.                  //第三次循环 limit + digit = -2147483635;
  73.  
  74.                      throw NumberFormatException.forInputString(s);
  75.                  }
  76.                  result -= digit;
  77.                  //第一次循环 result = -2;
  78.                  //第二次循环 result = -25;
  79.                  //第三次循环 result = -256;
  80.  
  81.              }
  82.          } else {
  83.              throw NumberFormatException.forInputString(s);
  84.          }
  85.          return negative ? result : -result;
  86.          //negative 值为true,所以 result = -256 = -256 返回结果
  87.      }

从以上代码可以看出 multmin 和result 都为负值 这样设计的原因我猜测是

/

Accumulating negatively avoids surprises near MAX_VALUE

(累加负值避免超过最大值 最小值:-2147483648 最大值:2147483647)

利用negative 这个标志变量,很巧妙的区分开了正负。

 

Integer.parseInt()到底有什么用?

Integer.parseInt() 是Integer包装类下的一个方法,作用是将()内的String类型字符串转化为int类型

 

示例1


 
  1. String str = "1234";
  2. int x = Integer.parseInt(str); //x的值为1234

Integer.parseInt()方法中要求的是()内的字符串必须是是数字,但其第一个数字前可以带 ‘-’ (负号)

 

示例2


 
  1. String str = "-1234";
  2. int x = Integer.parseInt(str); //x的值为-1234

补充:

如果str中含有部分非数字元素(除’-’),则会抛出错误

你可能感兴趣的