String字符串转化为int类型

通常我们使用Integer的parseInt解析数字型字符串,

它的内部是怎么解析的呢?

 String num="-2147483645";
 int numInt=parseInt(num,10);

那么Integer中的parseInt方法是如何做的呢?

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 forInputString(s);

                if (len == 1) // Cannot have lone "+" or "-"
                    throw 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 forInputString(s);
                }
                if (result < multmin) {
                    throw  forInputString(s);
                }
                result *= radix;
                if (result < limit + digit) {
                    throw forInputString(s);
                }
                result -= digit;
            }
        } else {
            throw forInputString(s);
        }
        return negative ? result : -result;
    }

 

字符串转化为整型 Integer.parseInt(String s,int radix)

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

首先判断radix 基数是否大于2  并且小于36
 

        char firstChar = s.charAt(0);
            if (firstChar < '0') { // Possible leading "+" or "-"
                if (firstChar == '-') {
                    negative = true;
                    limit = Integer.MIN_VALUE;
                } else if (firstChar != '+')
                    throw forInputString(s);

                if (len == 1) // Cannot have lone "+" or "-"
                    throw forInputString(s);
                i++;
            }

然后判断是否有+或者-号 判断正负
limit 都是负数 整数的最小数

 

multmin = limit / radix;

这个很关键  先移除一位


while (i < len) {
通过位数循环 遍历所有的字符

digit = Character.digit(s.charAt(i++),radix);

获取下一位的字符串的数字

 

if (digit < 0) {
                    throw forInputString(s);
                }

如果小于0 那么就不是数字 那么抛出异常

if (result < multmin) {
                    throw  forInputString(s);
                }

如果最终的结果result超过multmin绝对值,咱么就异常了


咱们上面说了 multmin是少一位的最大值 为什么呢
因为result目前来说就是少一轮循环的结果,因为第一次循环的时候 result为0 还没有赋值呢

result *= radix;


这个是把结果result 进了一位 乘以底数radix,比如十进制的话 就是乘以10

if (result < limit + digit) {
                    throw forInputString(s);
                }


这个时候 再判断一下 如果result进位之后,result小于 最小的值加上下一位数字(因为都是负数 所以相当于当前的结果如果加上下一位数字 大于最大值的界限的时候 那么就抛出异常)

如果没有抛出异常 那么就将结果 result -= digit;

return negative ? result : -result;
最后就是将结果扶正!该是啥就是啥

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值