Parses the specified string as a signed decimal integer value. The ASCII character \u002d ('-') is recognized as the minus sign.
源码如下
public static int parseInt(String string, int radix) throws NumberFormatException {
if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) {
throw new NumberFormatException("Invalid radix: " + radix);
}
if (string == null) {
throw invalidInt(string);
}
int length = string.length(), i = 0;
if (length == 0) {
throw invalidInt(string);
}
boolean negative = string.charAt(i) == '-';
if (negative && ++i == length) {
throw invalidInt(string);
}
return parse(string, i, radix, negative);
}
private static int parse(String string, int offset, int radix, boolean negative) throws NumberFormatException {
int max = Integer.MIN_VALUE / radix;
int result = 0, length = string.length();
while (offset < length) {
int digit = Character.digit(string.charAt(offset++), radix);
if (digit == -1) {
throw invalidInt(string);
}
if (max > result) {
throw invalidInt(string);
}
int next = result * radix - digit;
if (next > result) {
throw invalidInt(string);
}
result = next;
}
if (!negative) {
result = -result;
if (result < 0) {
throw invalidInt(string);
}
}
return result;
}
没有太明白里面radix的作用。
2.Integer.getInteger()
Returns the Integer
value of the system property identified by string
. Returns null
if string
is null
or empty, if the property can not be found or if its value can not be parsed as an integer.
返回的是系统常量的int值
其源码是这样的
public static Integer getInteger(String string) {
if (string == null || string.length() == 0) {
return null;
}
String prop = System.getProperty(string);
if (prop == null) {
return null;
}
try {
return decode(prop);
} catch (NumberFormatException ex) {
return null;
}
}
所以,不要望文生义,用了后面这方法