Java判斷Integer類型的值是否相等


我們知道Integer是int的包裝類,在jdk1.5以上,可以實現自動裝箱拆箱,就是jdk里面會自動幫我們轉換,不需要我們手動去強轉,所以我們經常在這兩種類型中隨意寫,平時也沒什么注意 但Integer他是對象,我們知道 == 比較的是堆中的地址,但有個奇怪的事是, 如果 Integer a = 123, Integer b = 123,可以返回true,但如果Integer a = 12345, Integer b = 12345,返回false

public class Demo {
 
    public static void main(String[] args) {
        Integer c = -128;
        Integer d = -128;
        System.out.println("c == d: " + (c == d));
        System.out.println("c.equals(d): " + c.equals(d));
        System.out.println("c.intValue() == d.intValue(): " + (c.intValue() == d.intValue()));
        System.out.println("Objects.equals(c, d): " + Objects.equals(c, d));
        
        Integer e = 127;
        Integer f = 127;
        System.out.println("e == f: " + (e == f));
        System.out.println("e.equals(f): " + e.equals(f));
        System.out.println("e.intValue() == f.intValue(): " + (e.intValue() == f.intValue()));
        System.out.println("Objects.equals(e, f): " + Objects.equals(e, f));
        
        Integer g = 128;
        Integer h = 128;
        System.out.println("g == h: " + (g == h));
        System.out.println("g.equals(h): " + g.equals(h));
        System.out.println("g.intValue() == h.intValue():" + (g.intValue() == h.intValue()));
        System.out.println("Objects.equals(g, h): " + Objects.equals(g, h));
    }
}

結果如下:

c == d: true
c.equals(d): true
c.intValue() == d.intValue(): true
Objects.equals(c, d): true
e == f: true
e.equals(f): true
e.intValue() == f.intValue(): true
Objects.equals(e, f): true
g == h: false
g.equals(h): true
g.intValue() == h.intValue():true
Objects.equals(g, h): true

 

(1)當用“==”進行比較時,jvm默認是比較數據在java堆的地址。int是一種基本數據類型,jvm會自動將Integer轉成int數值進行比較。在Integer類中,有一個內部靜態類IntegerCache ,用來支持自動拆箱和裝箱,如下,數值范圍[-128,127]

/**
     * Cache to support the object identity semantics of autoboxing for values between
     * -128 and 127 (inclusive) as required by JLS.
     *
     * The cache is initialized on first usage.  The size of the cache
     * may be controlled by the -XX:AutoBoxCacheMax=<size> option.
     * During VM initialization, java.lang.Integer.IntegerCache.high property
     * may be set and saved in the private system properties in the
     * sun.misc.VM class.
     */
 
    private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];
 
        static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                int i = parseInt(integerCacheHighPropValue);
                i = Math.max(i, 127);
                // Maximum array size is Integer.MAX_VALUE
                h = Math.min(i, Integer.MAX_VALUE - (-low));
            }
            high = h;
 
            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);
        }
 
        private IntegerCache() {}
    }

默認IntegerCache.low 是-127,Integer.high是128,如果在這個區間[-128,127]內,他就會把變量i當做一個變量,放到內存中,用“==”比較是會得出true;但如果不在這個范圍內,就會去new一個Integer對象,當運用“==”時,會比較Integer兩個對象地址,得出false。

比較兩個Integer的值是否相同,方法比較多:

1、推薦用equals(),這個還可以避免一些空指針問題的出現。

2、或者使用Integer.intValue();這樣出來的就是int值,就可以直接比較了(可能會拋出空指針異常);

 

本文摘選兩篇文章,略有修改

---------------------
作者:木林森淼
來源:CSDN
原文:https://blog.csdn.net/yangfengjueqi/article/details/81121140 

--------------------- 

作者:不吃老鼠的小花貓 
來源:CSDN 
原文:https://blog.csdn.net/xiaojiesu/article/details/50215237 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM