设有下面两个赋值语句:
a = Integer.parseInt("1024");
b = Integer.valueOf("1024").intValue();
下述说法正确的是()public static int parseInt(String s)
public static Integer valueOf(int i)
public static Integer valueOf(String s)
public int intValue()
这种题目最好的方法就是直接查看api文档,如下:
int parseInt = Integer.parseInt("1024");
int intValue = Integer.valueOf("1024").intValue();
-------------------------------------------------------
public static int parseInt(String s,
int radix)
throws NumberFormatException
public static Integer valueOf(String s,
int radix)
throws NumberFormatException
public int intValue() {
return value;
}
public static int parseInt(String s, int radix) throws NumberFormatException {int result = 0;return negative ? result : -result;
public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }
public static Integer valueOf(String s) throws NumberFormatException { return Integer.valueOf(parseInt(s, 10)); }
public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }
public int intValue() { return value; }
private final int value;
parseInt() 方法用于将字符串参数作为有符号的十进制整数进行解析。
static int parseInt(String s) static int parseInt(String s, int radix)返回的是 int 数据类型
static Integer valueOf(int i) static Integer valueOf(String s) static Integer valueOf(String s, int radix)返回的是 Integer 对象类型
intValue() :
以 int 形式返回指定的数值。
public static int parseInt(String s) throws NumberFormatException { return parseInt(s,10); }//进入第一层 //进入return parseInt(s,10);中的parseInt(s,10)函数: public static int parseInt(String s, int radix) throws NumberFormatException { if (s == null) { throw new NumberFormatException("Cannot parse null string"); } 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"); } boolean negative = false; int i = 0, len = s.length(); int limit = -Integer.MAX_VALUE; if (len > 0) { char firstChar = s.charAt(0); if (firstChar < '0') { // Possible leading "+"&nbs***bsp;"-" if (firstChar == '-') { negative = true; limit = Integer.MIN_VALUE; } else if (firstChar != '+') { throw NumberFormatException.forInputString(s, radix); } if (len == 1) { // Cannot have lone "+"&nbs***bsp;"-" throw NumberFormatException.forInputString(s, radix); } i++; } int multmin = limit / radix; int result = 0; while (i < len) { // Accumulating negatively avoids surprises near MAX_VALUE int digit = Character.digit(s.charAt(i++), radix); if (digit < 0 || result < multmin) { throw NumberFormatException.forInputString(s, radix); } result *= radix; if (result < limit + digit) { throw NumberFormatException.forInputString(s, radix); } result -= digit; } return negative ? result : -result; } else { throw NumberFormatException.forInputString(s, radix); } }//返回值是int类型
public static Integer valueOf(String s) throws NumberFormatException { return Integer.valueOf(parseInt(s, 10)); }//这是进入第一层 //进入Integer.valueOf(parseInt(s, 10))中的valueOf: public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }
public int intValue() { return value; }//从这,我没有看出来什么返回的是int吗?应该是吧