java-string轉(zhuǎn)換成integer的方式及原理

1 Integer.parseInt(String str)方法

public static int parseInt(String s) throws NumberFormatException {
        //內(nèi)部默認調(diào)用parseInt(String s, int radix)基數(shù)設(shè)置為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");
        }
        //基數(shù)是否小于最小基數(shù)
        if (radix < Character.MIN_RADIX) {
            throw new NumberFormatException("radix " + radix +
                                            " less than Character.MIN_RADIX");
        }
        //基數(shù)是否大于最大基數(shù)
        if (radix > Character.MAX_RADIX) {
            throw new NumberFormatException("radix " + radix +
                                            " greater than Character.MAX_RADIX");
        }

        int result = 0;
        //是否時負數(shù)
        boolean negative = false;
        //char字符數(shù)組下標和長度
        int i = 0, len = s.length();
        //限制
        int limit = -Integer.MAX_VALUE;
        int multmin;
        int digit;
        //判斷字符長度是否大于0,否則拋出異常
        if (len > 0) {
            //第一個字符是否是符號
            char firstChar = s.charAt(0);
            //根據(jù)ascii碼表看出加號(43)和負號(45)對應(yīng)的
            //十進制數(shù)小于‘0’(48)的
            if (firstChar < '0') { // Possible leading "+" or "-"
                //是負號
                if (firstChar == '-') {
                    //負號屬性設(shè)置為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
                //返回指定基數(shù)中字符表示的數(shù)值。(此處是十進制數(shù)值)
                digit = Character.digit(s.charAt(i++),radix);
                //小于0,則為非radix進制數(shù)
                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);
        }
        //根據(jù)上面得到的是否負數(shù),返回相應(yīng)的值
        return negative ? result : -result;
    }

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

返回指定基數(shù)中字符表示的數(shù)值。

public static int digit(int codePoint, int radix) {
        //基數(shù)必須再最大和最小基數(shù)之間
        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和基數(shù)大小,輸出對應(yīng)值
            //通過我們parseInt對應(yīng)的基數(shù)值為10,
            //所以,只能在第一個判斷(字符在0-9字符之間)
            //中得到result值 否則后續(xù)程序會拋出異常
            return result < radix ? result : -1;
        }
        return digitImpl(codePoint, radix);
    }

4 總結(jié)

  1. parseInt(String s)--內(nèi)部調(diào)用parseInt(s,10)(默認為10進制)
  2. 正常判斷null,進制范圍,length等
  3. 判斷第一個字符是否是符號位
  4. 循環(huán)遍歷確定每個字符的十進制值
  5. 通過*= 和-= 進行計算拼接
  6. 判斷是否為負值 返回結(jié)果。

參考

string轉(zhuǎn)換成integer的方式及原理

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 前言 最先接觸編程的知識是在大學(xué)里面,大學(xué)里面學(xué)了一些基礎(chǔ)的知識,c語言,java語言,單片機的匯編語言等;大學(xué)畢...
    oceanfive閱讀 3,376評論 0 7
  • (一) java基礎(chǔ)面試知識點 1.java中==和equals和hashCode的區(qū)別 答:首先回答的是三個定義...
    一生有你_b4f6閱讀 638評論 1 1
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,642評論 18 399
  • 讀書的方法有很多,我最為推崇的,也是我自己一直使用的方法,是從李笑來老師專欄磨煉學(xué)習(xí)到的只字不差的閱讀方式。 只字...
    感想心得閱讀 501評論 0 1
  • 聽說你很是迷戀人世 總把自己補滿人家的墻壁 偷偷的借著皎潔的月光 女孩在角落里偷偷的哭泣 看慣了世間的風(fēng)風(fēng)雨雨 觸...
    壟上行云閱讀 387評論 2 1

友情鏈接更多精彩內(nèi)容