Integer类对象池与==问题:Integer a=34556,b=34556;但a==b为false
生活随笔
收集整理的這篇文章主要介紹了
Integer类对象池与==问题:Integer a=34556,b=34556;但a==b为false
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1. 為什么Integer a=34556,b=34556;但a==b為false呢?
有些人不以為然的認(rèn)為,因?yàn)檫@是兩個(gè)對象,所以肯定是false。對于上面的問題,這個(gè)回答確實(shí)沒有問題。
那么若Integer a=3,b=3,則a==b是false還是true呢?
若還是按照上面的思路,那肯定也認(rèn)為是false。實(shí)際上,結(jié)果為true。
為什么呢?待我娓娓道來。
這是因?yàn)镮nteger類在內(nèi)存中有一個(gè)值的范圍為[-128,127]的對象池。
只要Integer對象的值在[-128,127]范圍內(nèi),都是從這個(gè)對象池中取。所以只要是這個(gè)范圍內(nèi)的Integer對象,只要值相同,就是同一個(gè)對象。那么==的結(jié)果,就是true。超過了這個(gè)范圍,則會(huì)new新的Integer對象,盡管值相同,但是已經(jīng)是不同的對象了。
驗(yàn)證代碼如下:
public class Test2 {public static void main(String[] args) {/*** 原始數(shù)據(jù)類型的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.* 意思是:原始數(shù)據(jù)類型的包裝類的equals方法比較的是數(shù)值,不會(huì)比較內(nèi)存地址(前提:變量不為null)* * Integer類型,有默認(rèn)的對象池,范圍是[-128,127]; 兩個(gè)值相等的Integer類型數(shù)據(jù)a和b, 1.若值在對象池范圍內(nèi)* 1)a==b結(jié)果為true 2)a.equals(b)結(jié)果為:true 2.若值不在對象池范圍內(nèi) 1)a==b結(jié)果為false* 2)a.equals(b)結(jié)果為:true*///1.值相同的兩個(gè)Integer類型變量與==以及equals()方法關(guān)系Integer a = 34556;Integer b = 34556;// Integer上限System.out.println("Integer上限:" + Integer.MAX_VALUE);// Integer類型數(shù)據(jù)的值和哈希值System.out.println("Integer類型數(shù)據(jù)的值和哈希值");System.out.println("a數(shù)值:" + a);System.out.println("a的哈希值:" + a.hashCode());System.out.println("b數(shù)值:" + b);System.out.println("b的哈希值:" + b.hashCode());System.out.println();System.out.println("a和b的==比較結(jié)果:" + (a == b));System.out.println("a和b的equals比較結(jié)果:" + (a.equals(b)));System.out.println("------------------------------------------------");Integer c = 1;Integer d = 1;System.out.println("c=" + c + ",d=" + d + "---c和d的==比較結(jié)果:" + (c == d));System.out.println("c=" + c + ",d=" + d + "---c和d的equals比較結(jié)果:" + (c.equals(d)));System.out.println("------------------------------------------------");Integer e = 127;Integer f = 127;System.out.println("e=" + e + ",f=" + f + "---e和f的==比較結(jié)果:" + (e == f));System.out.println("e=" + e + ",f=" + f + "---e和f的equals比較結(jié)果:" + (e.equals(f)));System.out.println("------------------------------------------------");Integer e1 = 128;Integer f1 = 128;System.out.println("e1=" + e1 + ",f1=" + f1 + "---e1和f1的==比較結(jié)果:" + (e1 == f1));System.out.println("e1=" + e1 + ",f1=" + f1 + "---e1和f1的equals比較結(jié)果:" + (e1.equals(f1)));System.out.println("------------------------------------------------");Integer e2 = -128;Integer f2 = -128;System.out.println("e2=" + e2 + ",f2=" + f2 + "---e2和f2的==比較結(jié)果:" + (e2 == f2));System.out.println("e2=" + e2 + ",f2=" + f2 + "---e2和f2的equals比較結(jié)果:" + (e2.equals(f2)));System.out.println("------------------------------------------------");Integer e3 = -129;Integer f3 = -129;System.out.println("e3=" + e3 + ",f3=" + f3 + "---e3和f3的==比較結(jié)果:" + (e3 == f3));System.out.println("e3=" + e3 + ",f3=" + f3 + "---e3和f3的equals比較結(jié)果:" + (e3.equals(f3)));/*** Double類型,沒有默認(rèn)的對象池 兩個(gè)值相等的Double類型數(shù)據(jù)a和b,無論它們一起取什么值 a==b結(jié)果為false* a.equals(b)結(jié)果為:true*/System.out.println("-----------------------Double類型-------------------------");Double e4 = 1.0;Double f4 = 1.0;System.out.println("e4=" + e4 + ",f4=" + f4 + "---e4和f4的==比較結(jié)果:" + (e4 == f4));System.out.println("e4=" + e4 + ",f4=" + f4 + "---e4和f4的equals比較結(jié)果:" + (e4.equals(f4)));}}測試結(jié)果
2.那么為什么所有的Integer類型且值相同的變量equals()均為true?
因?yàn)镮nteger包裝類的equals方法,比較的是對應(yīng)的int值。可以查看源碼。
也可以參考一下文章《深入理解Java原始數(shù)據(jù)類型和包裝類關(guān)于==和equals的比較》
這篇文章中有源碼可以參考。
3.只有Integer類有對象池,其他的Short…Double都沒有對象池
總結(jié)
以上是生活随笔為你收集整理的Integer类对象池与==问题:Integer a=34556,b=34556;但a==b为false的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深入理解Java原始数据类型和包装类关于
- 下一篇: lombok快速入门:实体类中再也不用写