Java 常用類 03. Java 整數(shù)緩沖區(qū)

問題:

  • 同樣為數(shù)字 100 為什么結(jié)果有時(shí)返回 false,有時(shí)返回 true,而用同樣的方法再使用 200,卻帶來不同的結(jié)果。具體看代碼及運(yùn)行結(jié)果。
public class Main {
    public static void main(String[] args) {
        Integer n1=new Integer(100);
        Integer n2=new Integer(100);
        System.out.println(n1==n2);
        Integer n3=100;    // 自動(dòng)裝箱
        Integer n4=100;
        System.out.println(n3==n4);
        Integer n5=200;    // 自動(dòng)裝箱
        Integer n6=200;
        System.out.println(n5==n6);
    }
}
  • 結(jié)果:

原因:

  • Java 預(yù)先創(chuàng)建了 256 個(gè)常用的整數(shù)包裝類型對(duì)象。(類似字符串中的字符串池,只是這個(gè)池中已經(jīng)有內(nèi)容了)
  • 在實(shí)際應(yīng)用當(dāng)中,對(duì)已創(chuàng)建的對(duì)象進(jìn)行復(fù)用。
  • 緩存區(qū)數(shù)組 [-128, 127]

分析:

  • n1、n2 比較:屬于兩個(gè)對(duì)象,存儲(chǔ)的地址不同,所以不相等返回 false。
  • n3、n4 和 n5、n6 通過自動(dòng)裝箱創(chuàng)建,但比較結(jié)果不同
  • 上面的代碼可以寫成:
public class Main {
    public static void main(String[] args) {
        Integer n1=new Integer(100);
        Integer n2=new Integer(100);
        System.out.println(n1==n2);
        // 自動(dòng)裝箱,Integer.valueOf() 為隱藏代碼
        Integer n3=Integer.valueOf(100);
        Integer n4=Integer.valueOf(100);
        System.out.println(n3==n4);
        // 自動(dòng)裝箱,Integer.valueOf() 為隱藏代碼
        Integer n5=Integer.valueOf(200);
        Integer n6=Integer.valueOf(200);
        System.out.println(n5==n6);
    }
}
  • 查看 valueOf() 源碼:
@IntrinsicCandidate
    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
  • 發(fā)現(xiàn):valueOf(int i) 方法中的參數(shù)值在 IntegerCache.lowIntegerCache.high 之間時(shí),會(huì)返回?cái)?shù)組 IntegerCache.cache[] 中的值。
  • 查看 IntegerCache 類的源碼:
private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer[] cache;
        static Integer[] archivedCache;

        static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                try {
                    h = Math.max(parseInt(integerCacheHighPropValue), 127);
                    // Maximum array size is Integer.MAX_VALUE
                    h = Math.min(h, Integer.MAX_VALUE - (-low) -1);
                } catch( NumberFormatException nfe) {
                    // If the property cannot be parsed into an int, ignore it.
                }
            }
            high = h;

            // Load IntegerCache.archivedCache from archive, if possible
            CDS.initializeFromArchive(IntegerCache.class);
            int size = (high - low) + 1;

            // Use the archived cache if it exists and is large enough
            if (archivedCache == null || size > archivedCache.length) {
                Integer[] c = new Integer[size];
                int j = low;
                for(int i = 0; i < c.length; i++) {
                    c[i] = new Integer(j++);
                }
                archivedCache = c;
            }
            cache = archivedCache;
            // range [-128, 127] must be interned (JLS7 5.1.7)
            assert IntegerCache.high >= 127;
        }

        private IntegerCache() {}
    }
  • 從源碼中可以看出 IntegerCache.low==-128IntegerCache.high==127,同時(shí)可以看出,數(shù)組 cache[]大小為 256,存放的元素為 {-128,-127,…,126,127}。
  • 因?yàn)?100 在 IntegerCache.low~IntegerCache.high 之間,所以返回的是數(shù)組 cache[] 中的元素,所以在比較 n3 與 n4 時(shí),兩者指向的為同一元素,同一地址,而 200 并不在上面的范圍中,所以返回 new Interger(200)
  • 相當(dāng)于
public class Main{
    public static void main(String[] args) {
        Integer a[]={100};
        Integer n3=a[0];
        Integer n4=a[0];
        System.out.println(n3==n4);
        Integer n5=new Integer(200);
        Integer n6=new Integer(200);
        System.out.println(n5==n6);
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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