亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Java中Integer類型值相等判斷方法

瀏覽:3日期:2022-08-17 11:00:35
背景

本周開發中遇到一個很低級的問題,Integer包裝類的相等判斷,包裝類與基本數據類型的區別,應該大多數人在面試中經常被問到,但是有的時候大家都會煩這些看起來沒啥用的東西,面試前還需要去熟悉,博主之前也是這樣認為的,但是平時看一些理論性的東西,在方案探討或者可行性分析時是很必要的,廢話不多少,看看這個問題吧

事故現場

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這個數字,為啥要搜這個127,因為127是可以的,128就不行,Integer肯定是對127做了特殊處理,搜了一下之后,果然有發現這個數字都集中在一個叫做IntegerCache內部類中,代碼如下:

/** * 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() {} }

看這個內部類的描述翻譯過來為:

按照java語義規范要求,緩存以支持值在-128到127(包含)范圍內的自動裝箱對象的初始化緩存在第一次使用時初始化,緩存的大小由選項{@code -XX:AutoBoxCacheMax=<size>}控制,在虛擬機初始化期間,java.lang.Integer.IntegerCache.high參數或被復制并且保存在sun.misc.VM類的私有系統變量中

通俗的來說就是,在-128到127的范圍內,Integer不對創建對象,而是直接取系統緩存中的變量數據。

解決

針對包裝類最好全部使用equals進行判斷,Integer得equals方法如下:

public boolean equals(Object obj) { if (obj instanceof Integer) { return value == ((Integer)obj).intValue(); } return false; }

判斷類型后,將其裝換成int值進行大小的判斷,并返回結果

反思

一些理論雖然平時不是很有,但是如果對理論理解不到位,出現理解偏差,這種情況下產生的問題,一般走查代碼是很難排查出問題的。

總結

到此這篇關于Java中Integer類型值相等判斷方法的文章就介紹到這了,更多相關Java Integer類型值相等判斷內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Java
相關文章:
主站蜘蛛池模板: 成人影院在线观看kkk4444 | 国产精品亚洲一区二区在线观看 | 欧美极品福利视频在线播放 | 亚洲国产日韩欧美在线a乱码 | 免费网站成人亚洲 | 国产婷婷丁香久久综合 | 国产网站免费观看 | 韩日福利视频 | 亚洲瑟 | 一级爱片 | 国产欧美在线亚洲一区刘亦菲 | 中文字幕久久网 | 欧美一级毛片免费高清aa | 国产午夜亚洲精品久久999 | 免费观看成人欧美1314www | 日韩一级片在线免费观看 | 97干视频| 成年人网址在线观看 | 免费一级毛片在线播放欧美 | 236理论片在线播放 24小时中文乱码字幕在线观看 | 口国产成人高清在线播放 | 日韩天天摸天天澡天天爽视频 | 成人三级在线播放 | 一级欧美在线的视频 | 国产成人综合91精品 | 人成在线 | 国产在线观看黄色 | 久久中文字幕综合不卡一二区 | 亚洲欧美日韩在线观看你懂的 | 国产精品亚洲精品不卡 | 亚洲hd| 日韩视频中文字幕专区 | 亚洲网红精品大秀在线观看 | 一级片在线免费播放 | 可以直接看的黄色网址 | 看真人视频一级毛片 | 在线日韩亚洲 | 久草97| 午夜视频网站在线观看 | 91亚洲国产系列精品第56页 | 成年美女黄网站色大免费视频 |