漫画:Integer 竟然有 4 种比较方法?
代碼測試
public?class?IntegerTest?{public?static?void?main(String[]?args)?{Integer?i1?=?127;Integer?i2?=?127;System.out.println(i1?==?i2);Integer?i3?=?128;Integer?i4?=?128;System.out.println(i3?==?i4);} }以上代碼的執(zhí)行結(jié)果為:
true
false
首先,當(dāng)我們將以上的測試代碼編譯為字節(jié)碼(.class)之后,編碼的代碼如下:
public?class?IntegerTest?{public?static?void?main(String[]?paramArrayOfString)?{Integer?integer1?=?Integer.valueOf(127);Integer?integer2?=?Integer.valueOf(127);System.out.println((integer1?==?integer2));Integer?integer3?=?Integer.valueOf(128);Integer?integer4?=?Integer.valueOf(128);System.out.println((integer3?==?integer4));} }可以看出在創(chuàng)建 Integer 時(shí)使用到了 valueOf,它的實(shí)現(xiàn)源碼如下:
public?static?Integer?valueOf(int?i)?{if?(i?>=?IntegerCache.low?&&?i?<=?IntegerCache.high)return?IntegerCache.cache[i?+?(-IntegerCache.low)];return?new?Integer(i); }從上述源碼中可以看出這個(gè)方法中使用了 IntegerCache,IntegerCache 的源碼如下:
private?static?class?IntegerCache?{static?final?int?low?=?-128;static?final?int?high;static?final?Integer?cache[];static?{//?high?value?may?be?configured?by?propertyint?h?=?127;String?integerCacheHighPropValue?=sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");if?(integerCacheHighPropValue?!=?null)?{try?{int?i?=?parseInt(integerCacheHighPropValue);i?=?Math.max(i,?127);//?Maximum?array?size?is?Integer.MAX_VALUEh?=?Math.min(i,?Integer.MAX_VALUE?-?(-low)?-1);}?catch(?NumberFormatException?nfe)?{//?If?the?property?cannot?be?parsed?into?an?int,?ignore?it.}}high?=?h;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)assert?IntegerCache.high?>=?127;}private?IntegerCache()?{} }從上述源碼可以看出,在 Integer 的取值在 -128 到 127 之間時(shí),它會(huì)復(fù)用已有的對象,因此在 i1(127)和 i2 使用 == 對比時(shí)值才會(huì)為 true,而當(dāng)取值變?yōu)?128 時(shí),則執(zhí)行的結(jié)果為 false。
這一點(diǎn)其實(shí)在阿里巴巴的《Java開發(fā)手冊》中也有相應(yīng)的規(guī)定,規(guī)定的內(nèi)容如下:
【強(qiáng)制】所有整型包裝類對象之間值的比較,全部使用 equals 方法比較。
說明:對于 Integer var = ? 在 -128 至 127 之間的賦值,Integer 對象是在 IntegerCache.cache 產(chǎn)生, 會(huì)復(fù)用已有對象,這個(gè)區(qū)間內(nèi)的 Integer 值可以直接使用 == 進(jìn)行判斷,但是這個(gè)區(qū)間之外的所有數(shù)據(jù),都 會(huì)在堆上產(chǎn)生,并不會(huì)復(fù)用已有對象,這是一個(gè)大坑,推薦使用 equals 方法進(jìn)行判斷。
注意事項(xiàng)
不僅如此,當(dāng)我們使用 new Integer 時(shí),無論值為多少都不能使用 == 比較,示例代碼如下:
public?class?IntegerTest?{public?static?void?main(String[]?args)?{Integer?i1?=?new?Integer(127);Integer?i2?=?new?Integer(127);System.out.println(i1?==?i2);} }以上代碼的執(zhí)行結(jié)果為:
false
這是因?yàn)?new Integer 方法并沒有使用到 IntegerCache,而是直接創(chuàng)建了新對象,因此就不能用 == 比較了。
小貼士:== 是用來直接比對兩個(gè)對象的引用是否相同的,而 equals 則是用來對比兩個(gè)對象的值是否相同的。
其他比較方式
compareTo
因?yàn)?Integer 類實(shí)現(xiàn)了 Comparable 接口,因此我們可以使用 compareTo 來對比兩個(gè)值的大小,實(shí)現(xiàn)源碼如下:
public?final?class?Integer?extends?Number?implements?Comparable<Integer>?{//?忽略其他內(nèi)容 }compareTo 的使用如下:
public?class?IntegerTest?{public?static?void?main(String[]?args)?{Integer?i1?=?new?Integer(128);Integer?i2?=?new?Integer(128);System.out.println(i1.compareTo(i2));} }以上代碼的執(zhí)行結(jié)果為:
0
compareTo 的源碼如下:
public?int?compareTo(Integer?anotherInteger)?{return?compare(this.value,?anotherInteger.value); } public?static?int?compare(int?x,?int?y)?{return?(x?<?y)???-1?:?((x?==?y)???0?:?1); }由此可以看出 compareTo 的返回值總共有三個(gè):-1、0、1,其中 -1 表示前一值小于后一個(gè)值;0 表示兩個(gè)值相等;1 表示前一個(gè)值大于后一個(gè)值,因此我們用它來比較兩個(gè) Integer 的值是否相等。
直接運(yùn)算
compareTo 方法給我們了一個(gè)啟發(fā),我們可以直接將兩個(gè)值進(jìn)行相減,如果相減的值等于 0,則說明對比的兩個(gè)值是相同的,示例代碼如下:
public?class?IntegerTest?{public?static?void?main(String[]?args)?{Integer?i1?=?new?Integer(128);Integer?i2?=?new?Integer(128);System.out.println((i1?-?i2)?==?0);} }以上代碼的執(zhí)行結(jié)果為:
true
擴(kuò)展知識:IntegerCache 值域修改
IntegerCache 默認(rèn)的取值范圍為 -128 到 127,但我們可以通過設(shè)置啟動(dòng)參數(shù)來調(diào)整 IntegerCache 的最大緩存值,比如我們可以配置虛擬機(jī)的啟動(dòng)參數(shù) -XX:AutoBoxCacheMax=1000,此配置表示將緩存的最大值設(shè)置為 1000,如果是 Idea 的配置如下:
此時(shí)我們編寫一個(gè)測試代碼:
public?class?IntegerTest?{public?static?void?main(String[]?args)?{Integer?i1?=?999;Integer?i2?=?999;System.out.println(i1?==?i2);} }以上代碼的執(zhí)行結(jié)果為:
true
從運(yùn)行的結(jié)果可以看出 IntegerCache 的取值范圍被成功的更改了。
總結(jié)
本文我們介紹了 Integer 的四種比較方式:==、equals、compareTo、直接運(yùn)算,而 == 方式并不能用于 Integer 的比較,它只適用于非 new Integer 的一定范圍內(nèi)(-128~127),而后三種方式都可以正常用于 Integer 的比較,其中 equals 的比較方式是最簡單也是最通用的。
互動(dòng)話題
除了以上幾種比較方式之外,你還知道其他的比較方式嗎?歡迎評論區(qū)補(bǔ)充留言。
最后的話原創(chuàng)不易,都看到這了,點(diǎn)個(gè)「在看」再走唄,這是對我最大的支持與鼓勵(lì),謝謝你!PS:公眾號推送最近改版了,朋友們設(shè)置為星標(biāo),防止錯(cuò)過精彩內(nèi)容。往期推薦漫畫:對象是如何被找到的?句柄 OR 直接指針?
漫畫:Java如何實(shí)現(xiàn)熱更新?
關(guān)注下方二維碼,每一天都有干貨!
點(diǎn)亮“在看”,助我寫出更多好文!
總結(jié)
以上是生活随笔為你收集整理的漫画:Integer 竟然有 4 种比较方法?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 面经 | 我是如何拿到阿里offer的?
- 下一篇: 消息队列终极解决方案——Stream(下