最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

關(guān)于Integer.parseInt()方法的使用

 更新時(shí)間:2022年11月21日 16:16:19   作者:diligence-zpf  
這篇文章主要介紹了關(guān)于Integer.parseInt()方法的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

解析Integer.parseInt()方法

我看到這個(gè)知識(shí)點(diǎn)是java面試基礎(chǔ)中的考點(diǎn),所以自己為了以后面試打算自己過一遍。

我看到別人博客上對(duì)源碼直接是文字說明,我覺得效果不是很好,我這里直接代數(shù)測(cè)試這個(gè)源碼運(yùn)行流程。

1.我?guī)胍粋€(gè)正正整數(shù) 256 注意看注釋中的數(shù)值

 public static int parseInt(String s, int radix)

    {
        /*
         * WARNING: This method may be invoked early during VM initialization
         * before IntegerCache is initialized. Care must be taken to not use
         * the valueOf method.
         */

        //判斷基數(shù)是否在 2~36之間
        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; //-2147483647
        int multmin;
        int digit;
        //字符串中的是否有符號(hào)
        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++;
            }
            //計(jì)算multmin 值
            multmin = limit / radix;
            // multmin = -214748364

           //開始循環(huán) 
            while (i < len) {
                // Accumulating negatively avoids surprises near MAX_VALUE
                //獲取字符轉(zhuǎn)換成對(duì)應(yīng)進(jìn)制的整數(shù)
                digit = Character.digit(s.charAt(i++),radix);
                //第一次循環(huán)  digit = 2;
                //第二次循環(huán)  digit = 5;
                //第三次循環(huán)  digit = 6;
                if (digit < 0) {
                    throw NumberFormatException.forInputString(s);
                }
                if (result < multmin) {
                    throw NumberFormatException.forInputString(s);
                }
                result *= radix;
                //第一次循環(huán)   result = 0;
                //第二次循環(huán)   result = -20;
                //第三次循環(huán)   result = -250;
                if (result < limit + digit) {
                //第一次循環(huán)  limit + digit = -2147483645;
                //第二次循環(huán)   limit + digit = -2147483640;
                //第三次循環(huán)   limit + digit = -2147483634;

                    throw NumberFormatException.forInputString(s);
                }
                result -= digit;
                //第一次循環(huán) result = -2;
                //第二次循環(huán) result = -25;
                //第三次循環(huán) result = -256;

            }
        } else {
            throw NumberFormatException.forInputString(s);
        }
        return negative ? result : -result;
        //negative 值為false,所以 -result = -(-256) = 256  返回結(jié)果
    }

2.我再帶入一個(gè)負(fù)數(shù) -256

 public static int parseInt(String s, int radix)

    {
        /*
         * WARNING: This method may be invoked early during VM initialization
         * before IntegerCache is initialized. Care must be taken to not use
         * the valueOf method.
         */

        //判斷基數(shù)是否在 2~36之間
        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; //-2147483647
        int multmin;
        int digit;
        //字符串中的是否有符號(hào)
        if (len > 0) {
            char firstChar = s.charAt(0);
            if (firstChar < '0') { // Possible leading "+" or "-"
                if (firstChar == '-') {
                //走這里
                    negative = true;
                    limit = Integer.MIN_VALUE;
               //此時(shí)  limit = -2147483648;
                } else if (firstChar != '+')
                    throw NumberFormatException.forInputString(s);

                if (len == 1) // Cannot have lone "+" or "-"
                    throw NumberFormatException.forInputString(s);
                i++;
            }
            //計(jì)算multmin 值
            multmin = limit / radix;
            // multmin = -214748364

           //開始循環(huán) 
            while (i < len) {
                // Accumulating negatively avoids surprises near MAX_VALUE
                //獲取字符轉(zhuǎn)換成對(duì)應(yīng)進(jìn)制的整數(shù)
                digit = Character.digit(s.charAt(i++),radix);
                //第一次循環(huán)  digit = 2;
                //第二次循環(huán)  digit = 5;
                //第三次循環(huán)  digit = 6;
                if (digit < 0) {
                    throw NumberFormatException.forInputString(s);
                }
                if (result < multmin) {
                    throw NumberFormatException.forInputString(s);
                }
                result *= radix;
                //第一次循環(huán)   result = 0;
                //第二次循環(huán)   result = -20;
                //第三次循環(huán)   result = -250;
                if (result < limit + digit) {
                //第一次循環(huán)  limit + digit = -2147483646;
                //第二次循環(huán)   limit + digit = -2147483641;
                //第三次循環(huán)   limit + digit = -2147483635;

                    throw NumberFormatException.forInputString(s);
                }
                result -= digit;
                //第一次循環(huán) result = -2;
                //第二次循環(huán) result = -25;
                //第三次循環(huán) result = -256;

            }
        } else {
            throw NumberFormatException.forInputString(s);
        }
        return negative ? result : -result;
        //negative 值為true,所以 result = -256 = -256  返回結(jié)果
    }

從以上代碼可以看出 multmin 和result 都為負(fù)值 這樣設(shè)計(jì)的原因我猜測(cè)是

Accumulating negatively avoids surprises near MAX_VALUE

(累加負(fù)值避免超過最大值 最小值:-2147483648 最大值:2147483647)

利用negative 這個(gè)標(biāo)志變量,很巧妙的區(qū)分開了正負(fù)。

Integer.parseInt()到底有什么用?

Integer.parseInt() 是Integer包裝類下的一個(gè)方法,作用是將()內(nèi)的String類型字符串轉(zhuǎn)化為int類型

示例1

String str = "1234";
int x = Integer.parseInt(str); ?//x的值為1234

Integer.parseInt()方法中要求的是()內(nèi)的字符串必須是是數(shù)字,但其第一個(gè)數(shù)字前可以帶 ‘-’ (負(fù)號(hào))

示例2

String str = "-1234";
int x = Integer.parseInt(str); ?//x的值為-1234

補(bǔ)充:

如果str中含有部分非數(shù)字元素(除’-’),則會(huì)拋出錯(cuò)誤

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java字符串轉(zhuǎn)時(shí)間簡(jiǎn)單示例代碼

    Java字符串轉(zhuǎn)時(shí)間簡(jiǎn)單示例代碼

    這篇文章主要給大家介紹了關(guān)于Java字符串轉(zhuǎn)時(shí)間的相關(guān)資料,在Java中字符和字符串常常需要相互轉(zhuǎn)化,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下
    2023-08-08
  • SpringBoot生成License的實(shí)現(xiàn)示例

    SpringBoot生成License的實(shí)現(xiàn)示例

    License指的是版權(quán)許可證,那么對(duì)于SpringBoot項(xiàng)目,如何增加License呢?本文就來介紹一下,感興趣的可以了解一下
    2021-06-06
  • 在Spring中使用JDBC和JDBC模板的講解

    在Spring中使用JDBC和JDBC模板的講解

    今天小編就為大家分享一篇關(guān)于在Spring中使用JDBC和JDBC模板的講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • 淺談java 重寫equals方法的種種坑

    淺談java 重寫equals方法的種種坑

    這篇文章主要介紹了淺談java 重寫equals方法的種種“坑”,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • SpringCloud整合Nacos實(shí)現(xiàn)流程詳解

    SpringCloud整合Nacos實(shí)現(xiàn)流程詳解

    這篇文章主要介紹了SpringCloud整合Nacos實(shí)現(xiàn)流程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Java SpringBoot容器注入對(duì)象詳解

    Java SpringBoot容器注入對(duì)象詳解

    本文通過實(shí)例代碼給大家詳解了springboot獲取ioc容器中注入的bean問題,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2021-09-09
  • SpringBoot接收J(rèn)SON類型的參數(shù)方式

    SpringBoot接收J(rèn)SON類型的參數(shù)方式

    這篇文章主要介紹了SpringBoot接收J(rèn)SON類型的參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • mybatis分頁效果實(shí)現(xiàn)代碼

    mybatis分頁效果實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了mybatis分頁效果的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • SpringBoot集成分頁插件PageHelper的配置和使用過程

    SpringBoot集成分頁插件PageHelper的配置和使用過程

    這篇文章主要介紹了SpringBoot集成分頁插件PageHelper的配置和使用過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • java Wrapper類基本用法詳解

    java Wrapper類基本用法詳解

    在本篇文章里小編給大家整理的是一篇關(guān)于java Wrapper類基本用法詳解,有興趣的朋友們可以參考下。
    2021-01-01

最新評(píng)論

泾阳县| 东兴市| 永城市| 商城县| 马龙县| 永登县| 韩城市| 高州市| 定安县| 合水县| 钦州市| 和田市| 梁平县| 九龙坡区| 资阳市| 谷城县| 韶关市| 海南省| 嫩江县| 宽甸| 安国市| 兴宁市| 固镇县| 玉门市| 东兰县| 中牟县| 大方县| 成安县| 烟台市| 博白县| 嘉峪关市| 禹州市| 澎湖县| 英山县| 万山特区| 静乐县| 林口县| 界首市| 西华县| 厦门市| 兰考县|