欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

判斷Integer類型的值是否相等

我們知道Integer是int的包裝類,在jdk1.5以上,可以實現(xiàn)自動裝箱拆箱,就是jdk里面會自動幫我們轉換,不需要我們手動去強轉,所以我們經(jīng)常在這兩種類型中隨意寫,平時也沒什么注意 但Integer他是對象,我們知道 == 比較的是堆中的地址,但有個奇怪的事是, 如果 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));
    }
}

結果如下:

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)當用“==”進行比較時,jvm默認是比較數(shù)據(jù)在java堆的地址。int是一種基本數(shù)據(jù)類型,jvm會自動將Integer轉成int數(shù)值進行比較。在Integer類中,有一個內(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() {}
    }

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

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

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

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

Integer賦值比較

Integer是int的包裝類,繼承Number類另實現(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();
 /**
  * 輸出結果為true----false----true,原因:
  * 1、 Integer為引用類型,引用類型棧中變量表示一個地址指向堆中的一片內(nèi)存空間
  * 2、 newInt.equals(num)為true是因為Integer重寫了equals方法,equals方法中實際是值比較
  * 3、(newInt == num)為false因為兩個變量不指向同一片內(nèi)存
  * 4、(intNum==num)結果為true是因為intNum是基本數(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) {
         //將對象強制轉換為Integer類型,然后自動拆箱進行==比較
            return value == ((Integer)obj).intValue();
        }
        return false;
    }

構造函數(shù)

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

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

相關文章

  • java必懂的冷知識點之Base64加密與解密

    java必懂的冷知識點之Base64加密與解密

    這篇文章主要介紹了java必懂的冷知識點之Base64加密與解密的相關資料,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • SpringBoot中的多個事務管理詳解

    SpringBoot中的多個事務管理詳解

    這篇文章主要介紹了SpringBoot中的多個事務管理詳解,事務管理是一種組織和協(xié)調各種活動和資源的方法,以實現(xiàn)特定目標,它涉及規(guī)劃、執(zhí)行和監(jiān)控各種任務,以確保項目或組織的順利運行,需要的朋友可以參考下
    2023-10-10
  • MybatisPlusInterceptor實現(xiàn)sql攔截器超詳細教程

    MybatisPlusInterceptor實現(xiàn)sql攔截器超詳細教程

    這篇文章主要給大家介紹了關于MybatisPlusInterceptor實現(xiàn)sql攔截器超詳細教程的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-08-08
  • Java forEach對原數(shù)組的操作過程

    Java forEach對原數(shù)組的操作過程

    forEach對于基本數(shù)據(jù)類型,是直接賦值,對于引用數(shù)據(jù)類型,是引用地址值,forEach遍歷時,是創(chuàng)建的臨時變量,引用的數(shù)據(jù)地址,本文給大家介紹Java forEach對原數(shù)組的操作過程,感興趣的朋友一起看看吧
    2024-02-02
  • spring-session簡介及實現(xiàn)原理源碼分析

    spring-session簡介及實現(xiàn)原理源碼分析

    這篇文章主要介紹了spring-session簡介及實現(xiàn)原理源碼分析,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • IDEA的.idea文件夾和iml文件使用方式

    IDEA的.idea文件夾和iml文件使用方式

    這篇文章主要介紹了IDEA的.idea文件夾和iml文件使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Java 使用Thumbnails對大圖片壓縮

    Java 使用Thumbnails對大圖片壓縮

    這篇文章主要介紹了Java 使用Thumbnails對大圖片壓縮,幫助大家更好的利用Java處理圖片,感興趣的朋友可以了解下
    2020-11-11
  • Java實現(xiàn)一個順序表的完整代碼

    Java實現(xiàn)一個順序表的完整代碼

    順序表是用一段物理地址連續(xù)的存儲單元依次存儲數(shù)據(jù)元素的線性結構,一般采用數(shù)組存儲。在數(shù)組上完成數(shù)據(jù)的增刪減改。順序表的底層是一個數(shù)組
    2021-04-04
  • 使用Java的Spring框架編寫第一個程序Hellow world

    使用Java的Spring框架編寫第一個程序Hellow world

    這篇文章主要介紹了Java的Spring框架并用其開始編寫第一個程序Hellow world的方法,Spring是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下
    2015-12-12
  • 解析Java編程中對于包結構的命名和訪問

    解析Java編程中對于包結構的命名和訪問

    這篇文章主要介紹了Java編程中對于包結構的命名和訪問,是Java入門學習中的基礎知識,需要的朋友可以參考下
    2015-12-12

最新評論