Understanding Java Source Code Integer.parseInt (String s, int radix)

I looked at the source code of the java.lang.Integer parseInt method.

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

I see that it is multminsomehow used to detect Integer overflow on both the negative and positive sides. But it’s hard for me to figure out how to do this.

I also do not understand why we store a negative result in the calculation, and then make it positive at the end, if it was not detected as a negative number.

+4
source share
2 answers

How does it work multmin?

multmin used in the following code:

if (result < multmin) {
    throw NumberFormatException.forInputString(s);
}
  • multmin, , :

    if result < multmin, ------> result < limit / radix (beacause multmin = limit / radix) ------> result * radix < limit ------> result * radix - digit < limit (overflow).

  • multmin, assert result * radix >= limit , , result * radix - digit

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

?

Integer.MIN_VALUE(-2147483648) , Integer.MAX_VALUE (2147483647).

, , '+', limit Integer.MAX_VALUE. , '-', limit 2147483648, .

+2

, s , [Integer.MIN_VALUE, Integer.MAX_VALUE] i.e. [-2147483648, 2147483647].

, . , .

, result + digit , :

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

, result * radix , - :

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

, , limit = Integer.MAX... multmin = limit / radix.

" "?

( ). , , - -2147483648; 2147483648, Integer.

-2147483648. , "", , :

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)
+2

Source: https://habr.com/ru/post/1692453/


All Articles