日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

Java判斷Integer相等-應該這樣用

來源: 責編: 時間:2023-09-22 20:10:28 304觀看
導讀先看下這段代碼,然后猜下結果:Integer i1 = 50;Integer i2 = 50;Integer i3 = 128;Integer i4 = 128;System.out.println(i1 == i2);System.out.println(i3 == i4);針對以上結果,估計不少Java小伙伴會算錯!如果在項目中使

先看下這段代碼,然后猜下結果:5KW28資訊網——每日最新資訊28at.com

Integer i1 = 50;Integer i2 = 50;Integer i3 = 128;Integer i4 = 128;System.out.println(i1 == i2);System.out.println(i3 == i4);

針對以上結果,估計不少Java小伙伴會算錯!5KW28資訊網——每日最新資訊28at.com

如果在項目中使用==對Integer進行比較,很容易掉坑。5KW28資訊網——每日最新資訊28at.com

5KW28資訊網——每日最新資訊28at.com

為什么發生以上結果?

1.執行Integer i1 = 50的時候,底層會進行自動裝箱:5KW28資訊網——每日最新資訊28at.com

Integer i1 = 50;//底層自動裝箱Integer i = Integer.valueOf(50);

2.再看==操作5KW28資訊網——每日最新資訊28at.com

==是判斷兩個對象在內存中的地址是否相等。所以System.out.println(i1 == i2); 和 System.out.println(i3 == i4); 是判斷他們在內存中的地址是否相等。5KW28資訊網——每日最新資訊28at.com

根據猜測應該全是false或者全是true呀,怎么會不同呢?5KW28資訊網——每日最新資訊28at.com

3.源碼底下無秘密5KW28資訊網——每日最新資訊28at.com

通過翻看jdk源碼,你會發現:如果要創建的 Integer 對象的值在 -128 到 127 之間,會從 IntegerCache 類中直接返回,否則才調用 new Integer方法創建。所以只要數值是正的Integer > 127,則會new一個新的對象。數值 <= 127時會直接從Cache中獲取到同一個對象。5KW28資訊網——每日最新資訊28at.com

public static Integer valueOf(int i) {    if (i >= IntegerCache.low && i <= IntegerCache.high)        return IntegerCache.cache[i + (-IntegerCache.low)];    return new Integer(i);}
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() {}}

結論

本文簡單分析了下Integer類型的==比較,解釋了為啥結果不一致,所以今后碰到Integer比較的時候,建議使用equals。5KW28資訊網——每日最新資訊28at.com

同理,Byte、Shot、Long等,也有Cache,各位記得翻看源碼!5KW28資訊網——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-11190-0.htmlJava判斷Integer相等-應該這樣用

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 為什么建議用const,enum,inline 替換 #define?

下一篇: String和Const char*參數類型選擇的合理性對比

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 精河县| 鹿泉市| 扬州市| 聊城市| 仙桃市| 梁河县| 庆安县| 荣昌县| 滨海县| 唐河县| 洛川县| 京山县| 西丰县| 西和县| 东安县| 平泉县| 常宁市| 纳雍县| 育儿| 鸡西市| 南乐县| 报价| 文成县| 闽清县| 禹城市| 江达县| 枣强县| 全椒县| 辉县市| 靖西县| 澄迈县| 雷州市| 岳阳市| 会昌县| 大丰市| 呼玛县| 抚宁县| 西和县| 平果县| 老河口市| 建湖县|