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,否则也会抛异常。  

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值