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,否則也會拋異常。  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章