java string强转integer_java-string转换成integer的方式及原理

1 Integer.parseInt(String str)方法

public static int parseInt(String s) throws NumberFormatException {

//内部默认调用parseInt(String s, int radix)基数设置为10

return parseInt(s,10);

}

2 Integer.parseInt(String s, int radix)方法

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.

*/

//判断字符是否为null

if (s == null) {

throw new NumberFormatException("s == 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;

//char字符数组下标和长度

int i = 0, len = s.length();

//限制

int limit = -Integer.MAX_VALUE;

int multmin;

int digit;

//判断字符长度是否大于0,否则抛出异常

if (len > 0) {

//第一个字符是否是符号

char firstChar = s.charAt(0);

//根据ascii码表看出加号(43)和负号(45)对应的

//十进制数小于‘0’(48)的

if (firstChar < '0') { // Possible leading "+" or "-"

//是负号

if (firstChar == '-') {

//负号属性设置为true

negative = true;

limit = Integer.MIN_VALUE;

}

//不是负号也不是加号则抛出异常

else if (firstChar != '+')

throw NumberFormatException.forInputString(s);

//如果有符号(加号或者减号)且字符串长度为1,则抛出异常

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);

//小于0,则为非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;

}

3 Character.digit(char ch, int radix)方法

返回指定基数中字符表示的数值。

public static int digit(int codePoint, int radix) {

//基数必须再最大和最小基数之间

if (radix < MIN_RADIX || radix > MAX_RADIX) {

return -1;

}

if (codePoint < 128) {

// Optimized for ASCII

int result = -1;

//字符在0-9字符之间

if ('0' <= codePoint && codePoint <= '9') {

result = codePoint - '0';

}

//字符在a-z之间

else if ('a' <= codePoint && codePoint <= 'z') {

result = 10 + (codePoint - 'a');

}

//字符在A-Z之间

else if ('A' <= codePoint && codePoint <= 'Z') {

result = 10 + (codePoint - 'A');

}

//通过判断result和基数大小,输出对应值

//通过我们parseInt对应的基数值为10,

//所以,只能在第一个判断(字符在0-9字符之间)

//中得到result值 否则后续程序会抛出异常

return result < radix ? result : -1;

}

return digitImpl(codePoint, radix);

}

4 总结

parseInt(String s)--内部调用parseInt(s,10)(默认为10进制)

正常判断null,进制范围,length等

判断第一个字符是否是符号位

循环遍历确定每个字符的十进制值

通过*= 和-= 进行计算拼接

判断是否为负值 返回结果。

参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值