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