编程语言
首页 > 编程语言> > 了解Java Integer.parseInt(String s,int radix)源代码

了解Java Integer.parseInt(String s,int radix)源代码

作者:互联网

我正在查看java.lang.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("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;
    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 NumberFormatException.forInputString(s);

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

我可以看到multmin以某种方式用于检测负侧和正侧的整数溢出.但我很难理解如何.

我也不明白为什么我们在计算时保持结果为负,如果没有被检测为负数则在最后使结果为正.

解决方法:

如果s表示在[Integer.MIN_VALUE,Integer.MAX_VALUE]之外的整数,即[-2147483648,2147483647]范围,则​​此方法用于抛出异常.

该算法执行重复的乘法和加法,最终可能导致溢出.该算法通过预先检查操作数来避免溢出.

检查溢出

检查结果数字是否会导致溢出而不实际添加它们的最简单方法是检查:

if (result > limit - digit) // result, limit and digit are positive

检查result * radix是否会导致溢出而不实际乘以它们的最简单方法是检查:

if (result > limit / radix) // result, limit and radix are positive

所以这解释了什么限制= Integer.MAX …和multmin = limit / radix do.

为什么“积极消极”?

该算法将符号分离出来并对其余数字进行操作(处理一个案例更容易).它必须处理的一个特例是-2147483648;在这种情况下,限制必须设置为2147483648,它超出了Integer的范围.

负累积时,限制可以设置为-2147483648.请注意,必须针对负数调整上述“if”条件,如下所示:

if (result < limit + digit) // result and limit are negative
if (result < limit / radix) // result and limit are negative

以下是每个步骤在算法内部发生的概述:

// parseInt("123", 10)
  limit: -2147483647 (-Integer.MAX_VALUE)
multmin: -214748364
 result: -1
 result: -12
 result: -123

// parseInt("2147483648", 10)
  limit: -2147483647 (-Integer.MAX_VALUE)
multmin: -214748364
 result: -2
 result: -21
 result: -214
 result: -2147
 result: -21474
 result: -214748
 result: -2147483
 result: -21474836
 result: -214748364
 result: Overflow (after multiplication, before subtraction)

标签:java,integer,integer-overflow
来源: https://codeday.me/bug/20190622/1263210.html