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

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

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

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

先看下這段代碼,然后猜下結果:NfL28資訊網——每日最新資訊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小伙伴會算錯!NfL28資訊網——每日最新資訊28at.com

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

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

為什么發生以上結果?

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

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

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

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

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

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

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

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

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

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

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

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

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 张家港市| 政和县| 台安县| 左云县| 浦江县| 宁波市| 定西市| 晋州市| 什邡市| 上虞市| 通海县| 垣曲县| 郸城县| 合水县| 怀仁县| 微山县| 金堂县| 景泰县| 宜丰县| 荃湾区| 岳普湖县| 南漳县| 大关县| 驻马店市| 赣州市| 余干县| 临汾市| 永德县| 漯河市| 胶州市| 霍城县| 比如县| 昌乐县| 永宁县| 中方县| 长白| 岱山县| 沾益县| 乌兰县| 江城| 饶阳县|