Java中Integer類(lèi)型值相等判斷方法
背景
本周開(kāi)發(fā)中遇到一個(gè)很低級(jí)的問(wèn)題,Integer包裝類(lèi)的相等判斷,包裝類(lèi)與基本數(shù)據(jù)類(lèi)型的區(qū)別,應(yīng)該大多數(shù)人在面試中經(jīng)常被問(wèn)到,但是有的時(shí)候大家都會(huì)煩這些看起來(lái)沒(méi)啥用的東西,面試前還需要去熟悉,博主之前也是這樣認(rèn)為的,但是平時(shí)看一些理論性的東西,在方案探討或者可行性分析時(shí)是很必要的,廢話(huà)不多少,看看這個(gè)問(wèn)題吧
事故現(xiàn)場(chǎng)
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是怎么處理的。
打開(kāi)Integer類(lèi)全局搜索一下127這個(gè)數(shù)字,為啥要搜這個(gè)127,因?yàn)?27是可以的,128就不行,Integer肯定是對(duì)127做了特殊處理,搜了一下之后,果然有發(fā)現(xiàn)這個(gè)數(shù)字都集中在一個(gè)叫做IntegerCache內(nèi)部類(lè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() {} }
看這個(gè)內(nèi)部類(lèi)的描述翻譯過(guò)來(lái)為:
按照java語(yǔ)義規(guī)范要求,緩存以支持值在-128到127(包含)范圍內(nèi)的自動(dòng)裝箱對(duì)象的初始化
緩存在第一次使用時(shí)初始化,緩存的大小由選項(xiàng){@code -XX:AutoBoxCacheMax=<size>}控制,在虛擬機(jī)初始化期間,java.lang.Integer.IntegerCache.high參數(shù)或被復(fù)制并且保存在sun.misc.VM類(lèi)的私有系統(tǒng)變量中
通俗的來(lái)說(shuō)就是,在-128到127的范圍內(nèi),Integer不對(duì)創(chuàng)建對(duì)象,而是直接取系統(tǒng)緩存中的變量數(shù)據(jù)。
解決
針對(duì)包裝類(lèi)最好全部使用equals進(jìn)行判斷,Integer得equals方法如下:
public boolean equals(Object obj) { if (obj instanceof Integer) { return value == ((Integer)obj).intValue(); } return false; }
判斷類(lèi)型后,將其裝換成int值進(jìn)行大小的判斷,并返回結(jié)果
反思
一些理論雖然平時(shí)不是很有,但是如果對(duì)理論理解不到位,出現(xiàn)理解偏差,這種情況下產(chǎn)生的問(wèn)題,一般走查代碼是很難排查出問(wèn)題的。
總結(jié)
到此這篇關(guān)于Java中Integer類(lèi)型值相等判斷方法的文章就介紹到這了,更多相關(guān)Java Integer類(lèi)型值相等判斷內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java連接mysql數(shù)據(jù)庫(kù)代碼實(shí)例程序
這篇文章主要介紹了java連接mysql數(shù)據(jù)庫(kù)代碼實(shí)例程序,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11使用jenkins部署springboot項(xiàng)目的方法步驟
這篇文章主要介紹了使用jenkins部署springboot項(xiàng)目的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04spring?cloud?eureka?服務(wù)啟動(dòng)失敗的原因分析及解決方法
這篇文章主要介紹了spring?cloud?eureka?服務(wù)啟動(dòng)失敗的原因解析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03Spring Boot利用Thymeleaf發(fā)送Email的方法教程
spring Boot默認(rèn)就是使用thymeleaf模板引擎的,下面這篇文章主要給大家介紹了關(guān)于在Spring Boot中利用Thymeleaf發(fā)送Email的方法教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-08-08Java實(shí)現(xiàn)按權(quán)重隨機(jī)數(shù)
這篇文章主要介紹了Java實(shí)現(xiàn)按權(quán)重隨機(jī)數(shù),本文給出了提出問(wèn)題、分析問(wèn)題、解決問(wèn)題三個(gè)步驟,需要的朋友可以參考下2015-04-04Spring中的BeanFactory工廠(chǎng)詳細(xì)解析
這篇文章主要介紹了Spring中的BeanFactory工廠(chǎng)詳細(xì)解析,Spring的本質(zhì)是一個(gè)bean工廠(chǎng)(beanFactory)或者說(shuō)bean容器,它按照我們的要求,生產(chǎn)我們需要的各種各樣的bean,提供給我們使用,需要的朋友可以參考下2023-12-12詳解Spring Boot中如何自定義SpringMVC配置
這篇文章主要給大家介紹了關(guān)于Spring Boot中如何自定義SpringMVC配置的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-09-09SpringBoot+SpringSession+Redis實(shí)現(xiàn)session共享及唯一登錄示例
這篇文章主要介紹了SpringBoot+SpringSession+Redis實(shí)現(xiàn)session共享及唯一登錄示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04springboot項(xiàng)目使用nohup將日志指定輸出文件過(guò)大問(wèn)題及解決辦法
在Spring Boot項(xiàng)目中,使用nohup命令重定向日志輸出到文件可能會(huì)使日志文件過(guò)大,文章介紹了兩種解決方法:一是創(chuàng)建腳本直接清除日志文件,二是創(chuàng)建腳本保留部分日志內(nèi)容,并將這些腳本加入定時(shí)任務(wù)中,這可以有效控制日志文件的大小,避免占用過(guò)多磁盤(pán)空間2024-10-10