Integer中valueof() 和 parseInt()的區別


首先來看一下jdk中的parseInt源碼:

 1 public static int parseInt(String s, int radix)
 2                 throws NumberFormatException
 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         if (s == null) {
11             throw new NumberFormatException("null");
12         }
13 
14         if (radix < Character.MIN_RADIX) {
15             throw new NumberFormatException("radix " + radix +
16                                             " less than Character.MIN_RADIX");
17         }
18 
19         if (radix > Character.MAX_RADIX) {
20             throw new NumberFormatException("radix " + radix +
21                                             " greater than Character.MAX_RADIX");
22         }
23 
24         int result = 0;
25         boolean negative = false;
26         int i = 0, len = s.length();
27         int limit = -Integer.MAX_VALUE;
28         int multmin;
29         int digit;
30 
31         if (len > 0) {
32             char firstChar = s.charAt(0);
33             if (firstChar < '0') { // Possible leading "+" or "-"
34                 if (firstChar == '-') {
35                     negative = true;
36                     limit = Integer.MIN_VALUE;
37                 } else if (firstChar != '+')
38                     throw NumberFormatException.forInputString(s);
39 
40                 if (len == 1) // Cannot have lone "+" or "-"
41                     throw NumberFormatException.forInputString(s);
42                 i++;
43             }
44             multmin = limit / radix;
45             while (i < len) {
46                 // Accumulating negatively avoids surprises near MAX_VALUE
47                 digit = Character.digit(s.charAt(i++),radix);
48                 if (digit < 0) {
49                     throw NumberFormatException.forInputString(s);
50                 }
51                 if (result < multmin) {
52                     throw NumberFormatException.forInputString(s);
53                 }
54                 result *= radix;
55                 if (result < limit + digit) {
56                     throw NumberFormatException.forInputString(s);
57                 }
58                 result -= digit;
59             }
60         } else {
61             throw NumberFormatException.forInputString(s);
62         }
63         return negative ? result : -result;
64     }

  可見parseInt返回的是int類型,就是將字符串轉化為基礎類型int,當然jdk中重載了parseInt這個函數,下面我們看看其他的寫法:

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

  可見其實就是調用了上面的函數。

  下面來看一看valueof這個函數:

1 public static Integer valueOf(String s, int radix) throws NumberFormatException {
2         return Integer.valueOf(parseInt(s,radix));
3     }
4 
5 
6 public static Integer valueOf(String s) throws NumberFormatException {
7         return Integer.valueOf(parseInt(s, 10));
8     }

  很明顯valueof返回的是Integer對象,函數里面其實也是調用parseInt函數。到這里我們就知道了,valueof就是把parseInt返回值封裝成對象。在FIndBugs時可能會提醒你,能用parseInt就不用valueof,parseInt效率更高。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM