在介绍parseInt(String s)方法之前,首先进行一个基本的字符串到整型的转换:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str="2147483647";
Pattern pattern=Pattern.compile("(^[+|-])?\\d+");//用正则表达式判断是否输入符合规范
Matcher match=pattern.matcher(str);
if(match.matches()){
int a=Integer.parseInt(str);
System.out.println("a="+a);
}else{
System.out.println("输入有误!");
}
}
}
接下来分析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");
}
/*******<span style="font-family: Arial, Helvetica, sans-serif;">Character.MIN_RADIX=2,</span><span style="font-family: Arial, Helvetica, sans-serif;">Character.MAX_RADIX=36</span><span style="font-family: Arial, Helvetica, sans-serif;">******/ </span>
<span style="font-family: Arial, Helvetica, sans-serif;"> /**********对基数进行判断,如果radix不在范围[2,36]内,则抛出异常**********/</span>
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();//i作为指针,记录str中的字符
int limit = -Integer.MAX_VALUE;//(-2147483647)
int multmin;
int digit;
if (len > 0) {
char firstChar = s.charAt(0);
if (firstChar < '0') { // Possible leading "+" or "-" 判断字符串首位是否有符号位,‘-’的ASCII码为45,‘+’为43,‘0’为48
if (firstChar == '-') {
negative = true;//标志位表示负数
limit = Integer.MIN_VALUE;//如果字符串的首位字符为‘-’,此时str所能表示的最小数为-2147483648,则limit替换为-2147483648
} else if (firstChar != '+')
throw NumberFormatException.forInputString(s);
if (len == 1) // Cannot have lone "+" or "-"
throw NumberFormatException.forInputString(s);
i++;
}
multmin = limit / radix;//multmin=-214748364
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) {//即result小于214748364,则与基数radix=10相乘后大于Integer所能表示的最大范围
throw NumberFormatException.forInputString(s);
}
result *= radix;
//即result小于-2147483647+digit(firstChar=='-',时limit=-2147483648)
/**满足上述条件则进行result-=digit;操作,否则超越Integer表示范围,抛出NumberFormatException异常**/
if (result < limit + digit) {
throw NumberFormatException.forInputString(s); }
result -= digit; }
}
else { throw NumberFormatException.forInputString(s); } return negative ? result : -result;
}
parseInt("0", 10) returns 0
* parseInt("473", 10) returns 473
* parseInt("+42", 10) returns 42
* parseInt("-0", 10) returns 0
* parseInt("-FF", 16) returns -255
* parseInt("1100110", 2) returns 102
* parseInt("2147483647", 10) returns 2147483647
* parseInt("-2147483648", 10) returns -2147483648
* parseInt("2147483648", 10) throws a NumberFormatException
* parseInt("99", 8) throws a NumberFormatException
* parseInt("Kona", 10) throws a NumberFormatException
* parseInt("Kona", 27) returns 411787