最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java 如何判斷Integer類型的值是否相等

 更新時(shí)間:2021年09月18日 11:02:51   作者:ryelqy  
這篇文章主要介紹了Java 如何判斷Integer類型的值是否相等操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

判斷Integer類型的值是否相等

我們知道Integer是int的包裝類,在jdk1.5以上,可以實(shí)現(xiàn)自動裝箱拆箱,就是jdk里面會自動幫我們轉(zhuǎn)換,不需要我們手動去強(qiáng)轉(zhuǎn),所以我們經(jīng)常在這兩種類型中隨意寫,平時(shí)也沒什么注意 但I(xiàn)nteger他是對象,我們知道 == 比較的是堆中的地址,但有個(gè)奇怪的事是, 如果 Integer a = 123, Integer b = 123,可以返回true,但如果Integer a = 12345, Integer b = 12345,返回false

public class Demo { 
    public static void main(String[] args) {
        Integer c = -128;
        Integer d = -128;
        System.out.println("c == d: " + (c == d));
        System.out.println("c.equals(d): " + c.equals(d));
        System.out.println("c.intValue() == d.intValue(): " + (c.intValue() == d.intValue()));
        System.out.println("Objects.equals(c, d): " + Objects.equals(c, d));
        
        Integer e = 127;
        Integer f = 127;
        System.out.println("e == f: " + (e == f));
        System.out.println("e.equals(f): " + e.equals(f));
        System.out.println("e.intValue() == f.intValue(): " + (e.intValue() == f.intValue()));
        System.out.println("Objects.equals(e, f): " + Objects.equals(e, f));
        
        Integer g = 128;
        Integer h = 128;
        System.out.println("g == h: " + (g == h));
        System.out.println("g.equals(h): " + g.equals(h));
        System.out.println("g.intValue() == h.intValue():" + (g.intValue() == h.intValue()));
        System.out.println("Objects.equals(g, h): " + Objects.equals(g, h));
    }
}

結(jié)果如下:

c == d: true
c.equals(d): true
c.intValue() == d.intValue(): true
Objects.equals(c, d): true
e == f: true
e.equals(f): true
e.intValue() == f.intValue(): true
Objects.equals(e, f): true
g == h: false
g.equals(h): true
g.intValue() == h.intValue():true
Objects.equals(g, h): true

(1)當(dāng)用“==”進(jìn)行比較時(shí),jvm默認(rèn)是比較數(shù)據(jù)在java堆的地址。int是一種基本數(shù)據(jù)類型,jvm會自動將Integer轉(zhuǎn)成int數(shù)值進(jìn)行比較。在Integer類中,有一個(gè)內(nèi)部靜態(tài)類IntegerCache ,用來支持自動拆箱和裝箱,如下,數(shù)值范圍[-128,127]

/**
     * 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 -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) {
                int i = parseInt(integerCacheHighPropValue);
                i = Math.max(i, 127);
                // Maximum array size is Integer.MAX_VALUE
                h = Math.min(i, Integer.MAX_VALUE - (-low));
            }
            high = h;
 
            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);
        } 
        private IntegerCache() {}
    }

默認(rèn)IntegerCache.low 是-127,Integer.high是128,如果在這個(gè)區(qū)間[-128,127]內(nèi),他就會把變量i當(dāng)做一個(gè)變量,放到內(nèi)存中,用“==”比較是會得出true;但如果不在這個(gè)范圍內(nèi),就會去new一個(gè)Integer對象,當(dāng)運(yùn)用“==”時(shí),會比較Integer兩個(gè)對象地址,得出false。

比較兩個(gè)Integer的值是否相同,方法比較多:

1、推薦用equals(),這個(gè)還可以避免一些空指針問題的出現(xiàn)。

2、或者使用Integer.intValue();這樣出來的就是int值,就可以直接比較了(可能會拋出空指針異常);

Integer賦值比較

Integer是int的包裝類,繼承Number類另實(shí)現(xiàn)Comparable接口,其取值范圍為:-2147483648~2147483647

賦值操作

 Integer newInt = new Integer(10);
 //自動裝箱操作,編譯后class文件中為:Integer num = Integer.valueOf(10);
 Integer num = 10;
 //num.intValue()為解包操作
 int intNum = num.intValue();
 /**
  * 輸出結(jié)果為true----false----true,原因:
  * 1、 Integer為引用類型,引用類型棧中變量表示一個(gè)地址指向堆中的一片內(nèi)存空間
  * 2、 newInt.equals(num)為true是因?yàn)镮nteger重寫了equals方法,equals方法中實(shí)際是值比較
  * 3、(newInt == num)為false因?yàn)閮蓚€(gè)變量不指向同一片內(nèi)存
  * 4、(intNum==num)結(jié)果為true是因?yàn)閕ntNum是基本數(shù)據(jù)類型,值直接存在棧中,兩者比較的是值
  */
 System.out.println(newInt.equals(num)+"----"+(newInt == num)+"----"+(intNum==num));
    private final int value;
    public boolean equals(Object obj) {
        if (obj instanceof Integer) {
         //將對象強(qiáng)制轉(zhuǎn)換為Integer類型,然后自動拆箱進(jìn)行==比較
            return value == ((Integer)obj).intValue();
        }
        return false;
    }

構(gòu)造函數(shù)

 public Integer(int value) {
  //參數(shù)為int 直接賦值
  this.value = value;
 }
 public Integer(String s) throws NumberFormatException {
  //參數(shù)為String的情況下調(diào)用paseInt()方法進(jìn)行賦值,10表示為按10進(jìn)制轉(zhuǎn)換
  this.value = parseInt(s, 10);
 }

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Springmvc ViewResolver設(shè)計(jì)實(shí)現(xiàn)過程解析

    Springmvc ViewResolver設(shè)計(jì)實(shí)現(xiàn)過程解析

    這篇文章主要介紹了Springmvc ViewResolver設(shè)計(jì)實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • 解讀jdk動態(tài)代理為什么必須實(shí)現(xiàn)接口

    解讀jdk動態(tài)代理為什么必須實(shí)現(xiàn)接口

    這篇文章主要介紹了解讀jdk動態(tài)代理為什么必須實(shí)現(xiàn)接口問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Java logback日志的簡單使用

    Java logback日志的簡單使用

    這篇文章主要介紹了Java logback日志的使用詳解,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下
    2021-03-03
  • springboot在服務(wù)器上的幾種啟動方式(小結(jié))

    springboot在服務(wù)器上的幾種啟動方式(小結(jié))

    這篇文章主要介紹了springboot在服務(wù)器上的幾種啟動方式(小結(jié)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-09-09
  • Spring MVC的web.xml配置詳解

    Spring MVC的web.xml配置詳解

    這篇文章主要介紹了Spring MVC的web.xml配置詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06
  • 微信小程序之搜索分頁功能的實(shí)現(xiàn)代碼

    微信小程序之搜索分頁功能的實(shí)現(xiàn)代碼

    這篇文章主要介紹了微信小程序之搜索分頁功能的實(shí)現(xiàn)代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Java多線程實(shí)現(xiàn)同時(shí)輸出

    Java多線程實(shí)現(xiàn)同時(shí)輸出

    這篇文章主要介紹了Java多線程實(shí)現(xiàn)同時(shí)打印的相關(guān)資料,需要的朋友可以參考下
    2016-03-03
  • FeignClient如何共享Header及踩坑過程記錄

    FeignClient如何共享Header及踩坑過程記錄

    這篇文章主要介紹了FeignClient如何共享Header及踩坑過程記錄,
    2022-03-03
  • 深入了解為什么Java中只有值傳遞?

    深入了解為什么Java中只有值傳遞?

    這篇文章主要介紹了為什么 Java 中只有值傳遞?下面我們來簡單了解一下吧
    2019-05-05
  • 利用SpringBoot解決多個(gè)定時(shí)任務(wù)阻塞的問題

    利用SpringBoot解決多個(gè)定時(shí)任務(wù)阻塞的問題

    當(dāng)我們在Spring Boot應(yīng)用中使用多個(gè)定時(shí)任務(wù)時(shí),任務(wù)之間的阻塞可能是一個(gè)常見的問題,這可能會因任務(wù)之間的依賴、執(zhí)行時(shí)間過長或資源爭用等原因而發(fā)生,本文讓我們深入探討如何利用Spring Boot來解決多個(gè)定時(shí)任務(wù)阻塞的問題,感興趣的小伙伴跟著小編一起來看看吧
    2024-01-01

最新評論

炉霍县| 曲松县| 铁力市| 台安县| 丹东市| 都昌县| 综艺| 梅河口市| 高密市| 山阴县| 基隆市| 定日县| 克东县| 西乡县| 筠连县| 海南省| 古交市| 中江县| 大关县| 商河县| 永清县| 屯留县| 东乡族自治县| 宽城| 炉霍县| 屯门区| 阜平县| 潢川县| 长春市| 都匀市| 昭通市| 长垣县| 保亭| 烟台市| 莱阳市| 洛阳市| 鄂温| 偃师市| 广河县| 阿瓦提县| 滕州市|