Integer.parseInt(String s, int radix)方法介绍(修正版)

        先来说明一下Integer.parseInt(String s, int radix)的功能

        Integer.parseInt(String s, int radix)就是将整数字符串s(radix用来指明s是几进制)转换成10进制的整数,显然前提是s为整数字符串。比如 s可以为“1314520”、“5201314”等。不可以为“我爱你一生一世”或者“I love you  forever”等之类的非整数字符串。

       那么,Integer.pareseInt("10086",10)就是将10进制整数字符串“10086”转换成10进制的整数10086。(有些说法是为了便于表达)

       下面我们来了解一下它的具体的内部机制。

       jdk中 java.lang.Integer中的源码如下:

1、

 

  1. public static int parseInt(String s) throws NumberFormatException   
  2. {  
  3.     return parseInt(s,10);  
  4.  }  

 

2、

  1. public static int parseInt(String s, int radix)throws NumberFormatException  
  2.    {  
  3.        if (s == null) {  
  4.            throw new NumberFormatException("null");  
  5.        }  
  6.   
  7. if (radix < Character.MIN_RADIX) {          //Character.MIN_RADIX=2  
  8.     throw new NumberFormatException("radix " + radix +  
  9.                     " less than Character.MIN_RADIX");  
  10. }  
  11.   
  12. if (radix > Character.MAX_RADIX) {          //Character.MAN_RADIX=36  
  13.     throw new NumberFormatException("radix " + radix +  
  14.                     " greater than Character.MAX_RADIX");  
  15. }  
  16.   
  17. int result = 0;  
  18. boolean negative = false;  
  19. int i = 0, max = s.length();  
  20. int limit;  
  21. int multmin;  
  22. int digit;  
  23.   
  24. if (max > 0) {  
  25.     if (s.charAt(0) == '-') {  
  26.     negative = true;  
  27.     limit = Integer.MIN_VALUE;  
  28.     i++;  
  29.     } else {  
  30.     limit = -Integer.MAX_VALUE;  
  31.     }  
  32.     multmin = limit / radix;  
  33.     if (i < max) {  
  34.     digit = Character.digit(s.charAt(i++),radix);  
  35.     if (digit < 0) {  
  36.         throw NumberFormatException.forInputString(s);  
  37.     } else {  
  38.         result = -digit;  
  39.     }  
  40.     }  
  41.     while (i < max) {  
  42.     // Accumulating negatively avoids surprises near MAX_VALUE  
  43.     digit = Character.digit(s.charAt(i++),radix);  
  44.     if (digit < 0) {  
  45.         throw NumberFormatException.forInputString(s);  
  46.     }  
  47.     if (result < multmin) {  
  48.         throw NumberFormatException.forInputString(s);  
  49.     }  
  50.     result *= radix;  
  51.     if (result < limit + digit) {  
  52.         throw NumberFormatException.forInputString(s);  
  53.     }  
  54.     result -= digit;  
  55.     }  
  56. else {  
  57.     throw NumberFormatException.forInputString(s);  
  58. }  
  59. if (negative) {  
  60.     if (i > 1) {  
  61.     return result;  
  62.     } else {    /* Only got "-" */  
  63.     throw NumberFormatException.forInputString(s);  
  64.     }  
  65. else {  
  66.     return -result;  
  67. }  
  68.    }  

我们以Integer.parseInt("110",10)为例。内部的转换过程其实是这样的:
                   
            i=  1*10*10+1*10+0*1; 

若是   Integer.parseInt("111",2)呢?

显然么     i  = 1*2*2+1*2+1*1。为了便于理解,直接这样称呼它们吧: 10进制整数字符串“110”,2进制整数字符串“111”。这时候,还有个问题,就是可以写成Integer.parseInt(“119”, 2)吗?显然是不对滴!2进制数怎么可能出现9呢。如果这样写,系统会抛出java.lang.NumberFormatException异常。

细心的朋友会注意到,在函数内部有这样的约束条件:Character.MAX_RADIX >=  radix >= Character.MIN_RADIX 。                      
根据:Character.MIN_RADIX=2和Character.MAX_RADIX=36 则,parseInt(String s, int radix)参数中  
radix的范围是在2~36之间,超出范围会抛异常。其中s的长度也不能超出7,否则也会抛异常。  

 

posted @ 2013-07-19 18:03  坚固66  阅读(1588)  评论(0编辑  收藏  举报