欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

解讀Integer類的parseInt和valueOf的區(qū)別

 更新時(shí)間:2022年11月22日 10:26:05   作者:bai_student  
這篇文章主要介紹了解讀Integer類的parseInt和valueOf的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Integer類的parseInt和valueOf區(qū)別

我們平時(shí)應(yīng)該都用過或者見過parseInt和valueOf這兩個(gè)方法。一般我們是想把String類型的字符數(shù)字轉(zhuǎn)成int類型。從這個(gè)功能層面來說,這兩個(gè)方法都一樣,都可以勝任這個(gè)功能。

但是,我們進(jìn)入源碼,看下Integer類下這兩個(gè)方法

我們看parseInt()這個(gè)方法是如何實(shí)現(xiàn)的

public static int parseInt(String s) throws NumberFormatException {
? ? return parseInt(s,10);
}

我們?cè)倏磛alueOf()是如何實(shí)現(xiàn)的

public static Integer valueOf(String s) throws NumberFormatException {
? ? return Integer.valueOf(parseInt(s, 10));
}

從代碼,我們起碼看到了兩點(diǎn):返回結(jié)果類型不一樣,parseInt方法返回的是int基本類型,valueOf方法返回的是Integer的包裝類型

valueOf方法實(shí)際上是調(diào)用了parseInt方法,也就是說,如果我們僅僅只需要得到字符串類型字符數(shù)值對(duì)應(yīng)的整數(shù)數(shù)值,那我們大可不必調(diào)用valueOf,因?yàn)檫@樣得到整形數(shù)值之后還要做一個(gè)裝箱的操作,將int封裝為Integer。

寫代碼測(cè)試效率:

public class StringDemo {
? ? public static void main(String[] args) {
? ? ? ? // TODO Auto-generated method stub
? ? ? ? String str = "123";
? ? ? ? long startTime = System.currentTimeMillis();
? ? ? ? for(int i = 0;i<100000000;i++){
? ? ? ? ? ? Integer.parseInt(str);
? ? ? ? }
? ? ? ? long endTime = System.currentTimeMillis();
? ? ? ? System.out.println(endTime-startTime);
? ? }
}

如下代碼:

public class StringDemo {
? ? public static void main(String[] args) {
? ? ? ? // TODO Auto-generated method stub
? ? ? ? String str = "123";
? ? ? ? long startTime = System.currentTimeMillis();
? ? ? ? for(int i = 0;i<100000000;i++){
? ? ? ? ? ? Integer.valueOf(str);
? ? ? ? }
? ? ? ? long endTime = System.currentTimeMillis();
? ? ? ? System.out.println(endTime-startTime);
? ? }
}

分別測(cè)試三遍,得到的時(shí)間如下,可以看到paraseInt()的效率更好。

方法第一次時(shí)長(zhǎng)第二次時(shí)長(zhǎng)第三次時(shí)長(zhǎng)
parseInt()294629652952
valueOf()312431173126

Integer的parseInt與value of原理

我一直使用Integer的轉(zhuǎn)換,包括Long,枚舉等,從來沒有注意它是怎么實(shí)現(xiàn)的,最近有個(gè)業(yè)務(wù)組轉(zhuǎn)換報(bào)錯(cuò)了,想看看是如何實(shí)現(xiàn)的。據(jù)筆者猜測(cè):ASCII碼轉(zhuǎn)換?這是常用的計(jì)量,什么大寫變小寫都是這樣實(shí)現(xiàn)的。下面看看如何實(shí)現(xiàn)的吧

1. demo構(gòu)建

public class StringParseInt {
    public static void main(String[] args) {
        String str = "-1234";
        int i = Integer.parseInt(str);
        int y = Integer.valueOf(str);
 
        System.out.println(i + "\t" + y);
    }
}

輸出都正常,關(guān)鍵是看怎么實(shí)現(xiàn)的

2. Integer的實(shí)現(xiàn)方式

2.1 value of 

    public static Integer valueOf(String s) throws NumberFormatException {
        return Integer.valueOf(parseInt(s, 10));
    }

本質(zhì)還是parseInt,從這點(diǎn)看與parseInt沒有區(qū)別;但是這里有裝箱,如果接收值是int建議直接使用parseInt,省去裝箱的過程。

    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

如果在緩存數(shù)組,直接使用,看看緩存數(shù)組怎么來的

    private static class IntegerCache {
        //下限固定-128
        static final int low = -128;
        //上限沒有初始化
        static final int high;
        //核心緩存數(shù)組
        static final Integer cache[];
        //類加載初始化
        static {
            // high value may be configured by property
            //初始127上限
            int h = 127;
            //VM參數(shù)java.lang.Integer.IntegerCache.high可以配置Integer的最大上限
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                try {
                    //我沒設(shè)置的值,如果不是int字符串型就會(huì)報(bào)錯(cuò),然后被捕獲
                    int i = parseInt(integerCacheHighPropValue);
                    //取大,對(duì)比127;也就是至少是127
                    i = Math.max(i, 127);
                    // Maximum array size is Integer.MAX_VALUE
                    // 上面官方注釋很明顯了,這里減128再減1是因?yàn)閘ow是-128
                    h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
                } catch( NumberFormatException nfe) {
                    // If the property cannot be parsed into an int, ignore it.
                }
            }
            high = h;
 
            //創(chuàng)建緩存數(shù)組,如果設(shè)置java.lang.Integer.IntegerCache.high,不宜設(shè)置過大,過大很占連續(xù)空間
            cache = new Integer[(high - low) + 1];
            int j = low;
            //初始化緩存
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);
 
            // range [-128, 127] must be interned (JLS7 5.1.7)
            //斷言至少127,JLS7
            assert IntegerCache.high >= 127;
        }
 
        private IntegerCache() {}
    }

從源碼看Integer內(nèi)部類裝載時(shí),會(huì)初始化一個(gè)緩存空間,存儲(chǔ)Integer對(duì)象。很多面試時(shí)就會(huì)被這個(gè)坑了,初始化的緩存對(duì)象地址取值是一致的,可以使用==作對(duì)比;然后超過這個(gè)范圍的Integer就不能了,要使用Integer的eq方法

    public boolean equals(Object obj) {
        if (obj instanceof Integer) {
            return value == ((Integer)obj).intValue();
        }
        return false;
    }

因?yàn)樽詣?dòng)裝箱,實(shí)際上使用的value of方法,默認(rèn)情況下,-128~127使用緩存對(duì)象。 

2.2 parseInt

    /**
     * 這段注釋尤為重要,定義了符號(hào)位,定義了10進(jìn)制數(shù)字
     * Parses the string argument as a signed decimal integer. The
     * characters in the string must all be decimal digits, except
     * that the first character may be an ASCII minus sign {@code '-'}
     * ({@code '\u005Cu002D'}) to indicate a negative value or an
     * ASCII plus sign {@code '+'} ({@code '\u005Cu002B'}) to
     * indicate a positive value. The resulting integer value is
     * returned, exactly as if the argument and the radix 10 were
     * given as arguments to the {@link #parseInt(java.lang.String,
     * int)} method.
     *
     * @param s    a {@code String} containing the {@code int}
     *             representation to be parsed
     * @return     the integer value represented by the argument in decimal.
     * @exception  NumberFormatException  if the string does not contain a
     *               parsable integer.
     */    
    public static int parseInt(String s) throws NumberFormatException {
        return parseInt(s,10);
    }

重點(diǎn)來哦,String能轉(zhuǎn)為int的本質(zhì),這里需要傳一個(gè)核心參數(shù),Integer給我們默認(rèn)了10進(jìn)制,其他類型也是相同,比如Long

    public static long parseLong(String s) throws NumberFormatException {
        return parseLong(s, 10);
    }

因?yàn)檗D(zhuǎn)換后的就是10進(jìn)制的數(shù)字,可供使用。

進(jìn)一步分析parseInt(String s, int radix);radix即進(jìn)制的意思。

    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("null");
        }
        //進(jìn)制不能小于2,至少要2進(jìn)制
        if (radix < Character.MIN_RADIX) {
            throw new NumberFormatException("radix " + radix +
                                            " less than Character.MIN_RADIX");
        }
        //進(jìn)制不能大于36
        if (radix > Character.MAX_RADIX) {
            throw new NumberFormatException("radix " + radix +
                                            " greater than Character.MAX_RADIX");
        }
 
        int result = 0;
        //正負(fù)標(biāo)記,默認(rèn)正
        boolean negative = false;
        //字符串長(zhǎng)度
        int i = 0, len = s.length();
        int limit = -Integer.MAX_VALUE;
        int multmin;
        int digit;
        //干活了
        if (len > 0) {
            //首位字符
            char firstChar = s.charAt(0);
            //這里玩了個(gè)計(jì)謀,0字符的ASCII是48,后面的數(shù)字包括ABCDEF的ASCII都比0大;
            //其中 + 43; - 45
            //只有帶符號(hào)位的會(huì)判斷,其他就默認(rèn)正數(shù)
            if (firstChar < '0') { // Possible leading "+" or "-"
                //負(fù)數(shù)
                if (firstChar == '-') {
                    negative = true;
                    limit = Integer.MIN_VALUE;
                //非負(fù)即正,因?yàn)樾∮?0'
                } 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
                // 拿到字符,轉(zhuǎn)換為ASCII數(shù)字并按進(jìn)制轉(zhuǎn)為數(shù)字
                digit = Character.digit(s.charAt(i++),radix);
                //不能帶符號(hào)位,前面已經(jīng)驗(yàn)證了
                if (digit < 0) {
                    throw NumberFormatException.forInputString(s);
                }
                //最小限制,但是這里是一個(gè)負(fù)數(shù)
                if (result < multmin) {
                    throw NumberFormatException.forInputString(s);
                }
                //由于是從高位向低位,所以需要進(jìn)制;字符每往后走,需要乘進(jìn)制
                result *= radix;
                //同樣最小驗(yàn)證,可能在某些地方有用,暫時(shí)沒看出來
                if (result < limit + digit) {
                    throw NumberFormatException.forInputString(s);
                }
                //這里使用反向進(jìn)位,為了照顧字符從前往后,也可以字符從后往前正向進(jìn)位
                result -= digit;
            }
        } else {
            throw NumberFormatException.forInputString(s);
        }
        //負(fù)數(shù)正數(shù)校正,上面的算法是反向進(jìn)位
        return negative ? result : -result;
    }

這里說一下Character.digit(s.charAt(i++),radix)

    public static int digit(char ch, int radix) {
        return digit((int)ch, radix);
    }

digit就是數(shù)字的意思,這里直接把char字符強(qiáng)轉(zhuǎn)int類型,即ASCII數(shù)字,然后按照進(jìn)制轉(zhuǎn)換成相應(yīng)的數(shù)字 

小結(jié):類型轉(zhuǎn)換其實(shí)是字符的ASCII的解析符號(hào)位,并按字符轉(zhuǎn)為數(shù)字,然后使用逆向負(fù)進(jìn)位方式生成數(shù)字,最后修正符號(hào)得出我們想要的結(jié)果。算法符合了字符串解析的順序,不符合人類的思維習(xí)慣。

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

相關(guān)文章

  • java實(shí)現(xiàn)簡(jiǎn)單單鏈表

    java實(shí)現(xiàn)簡(jiǎn)單單鏈表

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單單鏈表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-02-02
  • springAOP的三種實(shí)現(xiàn)方式示例代碼

    springAOP的三種實(shí)現(xiàn)方式示例代碼

    這篇文章主要介紹了springAOP的三種實(shí)現(xiàn)方式,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • Spring MVC 攔截器實(shí)現(xiàn)代碼

    Spring MVC 攔截器實(shí)現(xiàn)代碼

    本篇文章主要介紹了Spring MVC 攔截器的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-02-02
  • Java中輸入輸出方式詳細(xì)講解

    Java中輸入輸出方式詳細(xì)講解

    這篇文章主要給大家介紹了關(guān)于Java中輸入輸出方式的相關(guān)資料,Java輸入輸出是指使用java提供的一些類和方法來實(shí)現(xiàn)數(shù)據(jù)的輸入和輸出,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-09-09
  • 2021年最新Redis面試題匯總(3)

    2021年最新Redis面試題匯總(3)

    在程序員面試過程中redis相關(guān)的知識(shí)是常被問到的話題。這篇文章主要介紹了幾道Redis面試題,整理一下分享給大家,感興趣的小伙伴們可以參考一下
    2021-07-07
  • java并發(fā)包中CountDownLatch和線程池的使用詳解

    java并發(fā)包中CountDownLatch和線程池的使用詳解

    這篇文章主要介紹了java并發(fā)包中CountDownLatch和線程池的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • Spring MVC實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)增刪改查完整實(shí)例

    Spring MVC實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)增刪改查完整實(shí)例

    這篇文章主要介紹了Spring MVC實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)增刪改查完整實(shí)例,從創(chuàng)建一個(gè)web項(xiàng)目開始,分享了項(xiàng)目結(jié)構(gòu)以及具體Java代碼和前端頁(yè)面等相關(guān)內(nèi)容,具有一定借鑒價(jià)值,需要的朋友可以了解下。
    2017-12-12
  • 一文揭曉如何在Java中終止一個(gè)線程

    一文揭曉如何在Java中終止一個(gè)線程

    工作中我們經(jīng)常會(huì)用到線程,一般情況下我們讓線程執(zhí)行就完事了,那么你們有沒有想過如何去終止一個(gè)正在運(yùn)行的線程呢?本文就來帶大家一起看看
    2023-03-03
  • Spring+SpringMVC+Hibernate項(xiàng)目環(huán)境搭建的步驟(圖文)

    Spring+SpringMVC+Hibernate項(xiàng)目環(huán)境搭建的步驟(圖文)

    這篇文章主要介紹了Spring+SpringMVC+Hibernate項(xiàng)目環(huán)境搭建的步驟(圖文),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Java中使用COS實(shí)現(xiàn)文件上傳功能

    Java中使用COS實(shí)現(xiàn)文件上傳功能

    cos是O'Rrilly公司開發(fā)的一款用于HTTP上傳文件的OpenSource組件。下面通過本文給大家分享使用COS實(shí)現(xiàn)文件上傳功能,感興趣的朋友一起看看吧
    2017-08-08

最新評(píng)論