Java中Integer類型值相等判斷方法
背景
本周開發(fā)中遇到一個很低級的問題,Integer包裝類的相等判斷,包裝類與基本數(shù)據(jù)類型的區(qū)別,應(yīng)該大多數(shù)人在面試中經(jīng)常被問到,但是有的時候大家都會煩這些看起來沒啥用的東西,面試前還需要去熟悉,博主之前也是這樣認(rèn)為的,但是平時看一些理論性的東西,在方案探討或者可行性分析時是很必要的,廢話不多少,看看這個問題吧
事故現(xiàn)場
public static void main(String[] args) {
Integer a =127;
Integer b = 127;
Integer c = 128;
Integer d = 128;
Integer e = 129;
Integer f = 129;
System.out.println(a==b); //true
System.out.println(c==d);//false
System.out.println(e==f);//false
System.out.println(a.equals(b));//true
System.out.println(c.equals(d));//true
System.out.println(e.equals(f));//true
}
分析原因
上面例子中可以看到127的比較使用==是可以的,128和129就不可以,這種情況去看看Integer是怎么處理的。
打開Integer類全局搜索一下127這個數(shù)字,為啥要搜這個127,因為127是可以的,128就不行,Integer肯定是對127做了特殊處理,搜了一下之后,果然有發(fā)現(xiàn)這個數(shù)字都集中在一個叫做IntegerCache內(nèi)部類中,代碼如下:
/**
* 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 {@code -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) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = 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() {}
}
看這個內(nèi)部類的描述翻譯過來為:
按照java語義規(guī)范要求,緩存以支持值在-128到127(包含)范圍內(nèi)的自動裝箱對象的初始化
緩存在第一次使用時初始化,緩存的大小由選項{@code -XX:AutoBoxCacheMax=<size>}控制,在虛擬機初始化期間,java.lang.Integer.IntegerCache.high參數(shù)或被復(fù)制并且保存在sun.misc.VM類的私有系統(tǒng)變量中
通俗的來說就是,在-128到127的范圍內(nèi),Integer不對創(chuàng)建對象,而是直接取系統(tǒng)緩存中的變量數(shù)據(jù)。
解決
針對包裝類最好全部使用equals進(jìn)行判斷,Integer得equals方法如下:
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
判斷類型后,將其裝換成int值進(jìn)行大小的判斷,并返回結(jié)果
反思
一些理論雖然平時不是很有,但是如果對理論理解不到位,出現(xiàn)理解偏差,這種情況下產(chǎn)生的問題,一般走查代碼是很難排查出問題的。
總結(jié)
到此這篇關(guān)于Java中Integer類型值相等判斷方法的文章就介紹到這了,更多相關(guān)Java Integer類型值相等判斷內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot應(yīng)用程序同時支持HTTP和HTTPS協(xié)議的實現(xiàn)方法
如今,企業(yè)級應(yīng)用程序的常見場景是同時支持HTTP和HTTPS兩種協(xié)議,這篇文章考慮如何讓Spring Boot應(yīng)用程序同時支持HTTP和HTTPS兩種協(xié)議,需要的朋友可以參考下2019-10-10
springcloud中RabbitMQ死信隊列與延遲交換機實現(xiàn)方法
死信隊列是消息隊列中非常重要的概念,同時我們需要業(yè)務(wù)場景中都需要延遲發(fā)送的概念,比如12306中的30分鐘后未支付訂單取消,那么本期,我們就來講解死信隊列,以及如何通過延遲交換機來實現(xiàn)延遲發(fā)送的需求,感興趣的朋友一起看看吧2022-05-05
springboot+vue+elementsUI實現(xiàn)分角色注冊登錄界面功能
這篇文章主要給大家介紹了關(guān)于springboot+vue+elementsUI實現(xiàn)分角色注冊登錄界面功能的相關(guān)資料,Spring?Boot和Vue.js是兩個非常流行的開源框架,可以用來構(gòu)建Web應(yīng)用程序,需要的朋友可以參考下2023-07-07
深入淺析springsecurity入門登錄授權(quán)
SpringSecurity為我們提供了基于注解的權(quán)限控制方案,這也是我們項目中主要采用的方式,我們可以使用注解去指定訪問對應(yīng)的資源所需的權(quán)限,這篇文章主要介紹了springsecurity入門登錄授權(quán),需要的朋友可以參考下2024-05-05
springboot2.0如何通過fastdfs實現(xiàn)文件分布式上傳
這篇文章主要介紹了springboot2.0如何通過fastdfs實現(xiàn)文件分布式上傳,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12

