他们有本质区别,Integer.valueof(String s)是将一个包装类是将一个实际值为数字的变量先转成string型再将它转成Integer型的包装类对象(相当于转成了int的对象)这样转完的对象就具有方法和属性了。 而Integer.parseInt(String s)只是将是数字的字符串转成数字,注意他返回的是int型变量不具备方法和属性。
parseXXX()返回的是基本类型,例如parseInt()返回int型;
valueOf()返回的是对象类型,例如valueOf()返回Integer类型;
最容易被忽视的是:
被valueOf()转型的数值,如果超过【范围:128至127】,即使重新赋值给int型,也不能直接对比大小(编译能通过,但比较的结果是错的),不信试试。
parseInt源码: public static int parseInt(String s) throws NumberFormatException { return parseInt(s,10); } public static int parseInt(String s, int radix) throws NumberFormatException { /* * WARNING: This method may be invoked early during VM initialization * before IntegerCache is initialized. Care must be taken to not use * the valueOf method. */ if (s == null) { throw new NumberFormatException("null"); } if (radix < Character.MIN_RADIX) { throw new NumberFormatException("radix " + radix + " less than Character.MIN_RADIX"); } if (radix > Character.MAX_RADIX) { throw new NumberFormatException("radix " + radix + " greater than Character.MAX_RADIX"); } int result = 0; boolean negative = false; int i = 0, len = s.length(); int limit = -Integer.MAX_VALUE; int multmin; int digit; if (len > 0) { char firstChar = s.charAt(0); if (firstChar < '0') { // Possible leading "+" or "-" if (firstChar == '-') { negative = true; limit = Integer.MIN_VALUE; } else if (firstChar != '+') throw NumberFormatException.forInputString(s); if (len == 1) // Cannot have lone "+" or "-" throw NumberFormatException.forInputString(s); i++; } multmin = limit / radix; while (i < len) { // Accumulating negatively avoids surprises near MAX_VALUE digit = Character.digit(s.charAt(i++),radix); if (digit < 0) { throw NumberFormatException.forInputString(s); } if (result < multmin) { throw NumberFormatException.forInputString(s); } result *= radix; if (result < limit + digit) { throw NumberFormatException.forInputString(s); } result -= digit; } } else { throw NumberFormatException.forInputString(s); } return negative ? result : -result; } 当使用 public static int parseInt(String s) 方法的时候会去调用public static int parseInt(String s, int radix),这个方法可以将字符串解析成不同进制的int类型的数, 直接调用public static int parseInt(String s)会默认10进制。 valueOf源码: public static Integer valueOf(String s) throws NumberFormatException { return Integer.valueOf(parseInt(s, 10)); } 如果使用public static Integer valueOf(String s) 方法,两者的区别就在于返回值类型不同,parseInt返回值是int类型,valueOf返回值类型是Integer类型。 public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); } private static class IntegerCache { static final int low = -128; static final int high; static final Integer cache[]; static { // high value may be configured by property int h = 127; String integerCacheHighPropValue = sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); if (integerCacheHighPropValue != null) { try { int i = parseInt(integerCacheHighPropValue); i = Math.max(i, 127); // Maximum array size is Integer.MAX_VALUE h = Math.min(i, Integer.MAX_VALUE - (-low) -1); } catch( NumberFormatException nfe) { // If the property cannot be parsed into an int, ignore it. } } high = h; cache = new Integer[(high - low) + 1]; int j = low; for(int k = 0; k < cache.length; k++) cache[k] = new Integer(j++); // range [-128, 127] must be interned (JLS7 5.1.7) assert IntegerCache.high >= 127; } private IntegerCache() {} } 调用public static Integer valueOf(int i) 方法是,首先会调用IntegerCache(), IntegerCache()其源码中为 [-128 , 128) 之间的Integer对象进行了缓存以提高效率。 下面这题可以帮助理解: Integer a = 130; Integer b = Integer.valueOf(130); System.out.println(a == b); // 结果为false; Integer c = 20; Integer d = Integer.valueOf(20); System.out.println(c == d); // 结果为true; 原因是:如果整数的范围在[-128 , 128) 之间,会直接从缓存中取值,而如果整数的范围不在[-128 , 128)之间会在堆内存中new出一个新的Integer类型的对象。
