Integer类对象池与==问题:Integer a=34556,b=34556;但a==b为false
生活随笔
收集整理的這篇文章主要介紹了
Integer类对象池与==问题:Integer a=34556,b=34556;但a==b为false
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 為什么Integer a=34556,b=34556;但a==b為false呢?
有些人不以為然的認為,因為這是兩個對象,所以肯定是false。對于上面的問題,這個回答確實沒有問題。
那么若Integer a=3,b=3,則a==b是false還是true呢?
若還是按照上面的思路,那肯定也認為是false。實際上,結果為true。
為什么呢?待我娓娓道來。
這是因為Integer類在內存中有一個值的范圍為[-128,127]的對象池。
只要Integer對象的值在[-128,127]范圍內,都是從這個對象池中取。所以只要是這個范圍內的Integer對象,只要值相同,就是同一個對象。那么==的結果,就是true。超過了這個范圍,則會new新的Integer對象,盡管值相同,但是已經是不同的對象了。
驗證代碼如下:
public class Test2 {public static void main(String[] args) {/*** 原始數據類型的equals方法說明: Compares this object to the specified object. The* result is {@code true} if and only if the argument is not* {@code null} and is an {@code Integer} object that contains the same* {@code int} value as this object.* 意思是:原始數據類型的包裝類的equals方法比較的是數值,不會比較內存地址(前提:變量不為null)* * Integer類型,有默認的對象池,范圍是[-128,127]; 兩個值相等的Integer類型數據a和b, 1.若值在對象池范圍內* 1)a==b結果為true 2)a.equals(b)結果為:true 2.若值不在對象池范圍內 1)a==b結果為false* 2)a.equals(b)結果為:true*///1.值相同的兩個Integer類型變量與==以及equals()方法關系Integer a = 34556;Integer b = 34556;// Integer上限System.out.println("Integer上限:" + Integer.MAX_VALUE);// Integer類型數據的值和哈希值System.out.println("Integer類型數據的值和哈希值");System.out.println("a數值:" + a);System.out.println("a的哈希值:" + a.hashCode());System.out.println("b數值:" + b);System.out.println("b的哈希值:" + b.hashCode());System.out.println();System.out.println("a和b的==比較結果:" + (a == b));System.out.println("a和b的equals比較結果:" + (a.equals(b)));System.out.println("------------------------------------------------");Integer c = 1;Integer d = 1;System.out.println("c=" + c + ",d=" + d + "---c和d的==比較結果:" + (c == d));System.out.println("c=" + c + ",d=" + d + "---c和d的equals比較結果:" + (c.equals(d)));System.out.println("------------------------------------------------");Integer e = 127;Integer f = 127;System.out.println("e=" + e + ",f=" + f + "---e和f的==比較結果:" + (e == f));System.out.println("e=" + e + ",f=" + f + "---e和f的equals比較結果:" + (e.equals(f)));System.out.println("------------------------------------------------");Integer e1 = 128;Integer f1 = 128;System.out.println("e1=" + e1 + ",f1=" + f1 + "---e1和f1的==比較結果:" + (e1 == f1));System.out.println("e1=" + e1 + ",f1=" + f1 + "---e1和f1的equals比較結果:" + (e1.equals(f1)));System.out.println("------------------------------------------------");Integer e2 = -128;Integer f2 = -128;System.out.println("e2=" + e2 + ",f2=" + f2 + "---e2和f2的==比較結果:" + (e2 == f2));System.out.println("e2=" + e2 + ",f2=" + f2 + "---e2和f2的equals比較結果:" + (e2.equals(f2)));System.out.println("------------------------------------------------");Integer e3 = -129;Integer f3 = -129;System.out.println("e3=" + e3 + ",f3=" + f3 + "---e3和f3的==比較結果:" + (e3 == f3));System.out.println("e3=" + e3 + ",f3=" + f3 + "---e3和f3的equals比較結果:" + (e3.equals(f3)));/*** Double類型,沒有默認的對象池 兩個值相等的Double類型數據a和b,無論它們一起取什么值 a==b結果為false* a.equals(b)結果為:true*/System.out.println("-----------------------Double類型-------------------------");Double e4 = 1.0;Double f4 = 1.0;System.out.println("e4=" + e4 + ",f4=" + f4 + "---e4和f4的==比較結果:" + (e4 == f4));System.out.println("e4=" + e4 + ",f4=" + f4 + "---e4和f4的equals比較結果:" + (e4.equals(f4)));}}測試結果
2.那么為什么所有的Integer類型且值相同的變量equals()均為true?
因為Integer包裝類的equals方法,比較的是對應的int值。可以查看源碼。
也可以參考一下文章《深入理解Java原始數據類型和包裝類關于==和equals的比較》
這篇文章中有源碼可以參考。
3.只有Integer類有對象池,其他的Short…Double都沒有對象池
總結
以上是生活随笔為你收集整理的Integer类对象池与==问题:Integer a=34556,b=34556;但a==b为false的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深入理解Java原始数据类型和包装类关于
- 下一篇: Java源码解析:hashCode与相同