Java中Integer.parse()的學習總結

2014.12.29
java

下面的寫的總結是關於android源代碼中的實現。jdk1.8.0_25源代碼中的實現不一樣,可是思想相似,都是使用負數表示。我更喜歡android中的實現。android

內部使用負數表示

當函數還未考慮到符號影響時候,內部是用負數來表示逐步轉換的結果。git

初看到下面兩句,非常疑惑。less

int max = Integer.MIN_VALUE / radix;

int next = result * radix - digit;

爲何要用負數來表示呢?正數才比較符號日常頭腦的思路。ide

個人想法是,負數部分是0~-2147483648,而正數部分是0~2147483647,負數範圍比正數範圍廣。若是內部是用正數的話,"-2147483648"這個字符串處理就更復雜點,由於正數無法表示2147483648。函數

其餘的理解放在下面代碼註釋中(Android):spa

    private static int parse(String string, int offset, int radix, boolean negative) throws NumberFormatException {
    	// Why is Integer.MIN_VALUE is choosed? Not Integer.MAX_VALUE ?
    	// Maybe because the range in the minus side is greater than that in the plus side
        int max = Integer.MIN_VALUE / radix;
        int result = 0;
        int length = string.length();
        while (offset < length) {
            int digit = Character.digit(string.charAt(offset++), radix);
            if (digit == -1) {
                throw invalidInt(string);
            }
            //若是此時的result的絕對值已經大於max的絕對值,那麼result再增長一位必超出範圍。
            //int max = Integer.MIN_VALUE / radix; 這是max定義。
            if (max > result) {
                throw invalidInt(string);
            }
            int next = result * radix - digit;
            //可能出現overflow的現象。
            //如:若是radix爲10,上面result等於-214748364,又digit大於8,則next會超出範圍。
            if (next > result) {
                throw invalidInt(string);
            }
            result = next;
        }
        if (!negative) {
            result = -result;
            // when result equals to 80000000H, -result equals to result.
            if (result < 0) {
                throw invalidInt(string);
            }
        }
        return result;
    }

如下是jdk1.8.0_25中的源代碼code

    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 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;
    }
相關文章
相關標籤/搜索