int與Integer詳解(java基礎(chǔ)篇)

前言

在回顧==與equals的區(qū)別于聯(lián)系時(shí),我們很清楚的了解到:
==:可以用于基本數(shù)據(jù)類型的比較,也可以用于對(duì)象進(jìn)行比。,對(duì)于基本數(shù)據(jù)類型,比較的是值;對(duì)于引用數(shù)據(jù)類型,比較的是內(nèi)存地址
equals:Object類的Native方法,本質(zhì)等同于“==”,比較內(nèi)存地址;但我們常用的String、Integer等都對(duì)equals方法進(jìn)行了重寫(xiě),使其變成了值的比較。
那么就會(huì)有疑問(wèn)了,那基本類型和引用類型的比較呢,這就涉及到我們的八種基本數(shù)據(jù)類型及它們的封裝數(shù)據(jù)類型的對(duì)比。下面我們根據(jù)實(shí)例講解:

        int int1 = 2;
        int int2 = 2;
        Integer integer1 = 2;
        Integer integer2 = 2;
        Integer newInteger1 = new Integer(2);
        Integer newInteger2 = new Integer(2);
        //1
        System.out.println("int1與int2的==:" + (int1 == int2));//true
        //int1與int2是基本數(shù)據(jù)類型,不能使用。equals對(duì)比
        //2
        System.out.println("integer1與integer2的==:" + (integer1 == integer2));//true
        //3
        System.out.println("integer1與integer2的equals:" + (integer1.equals(integer2)));//true

        //4
        System.out.println("newInteger1與newInteger2的==:" + (newInteger1==newInteger2));//false
        //5
        System.out.println("newInteger1與newInteger2的equals:" + (newInteger1.equals(newInteger2)));//true
        //6
        System.out.println("int1與integer1的==:" + (int1==integer1));//true
        //7
        System.out.println("int1與integer1的equals:" + (integer1.equals(int1)));//true
        
        //8
        System.out.println("int1與newInteger1的==:" + (int1==newInteger1));//true

        //9
        System.out.println("integer1與newInteger1的==:" + (integer1==newInteger1));//false
        //10
        System.out.println("integer1與newInteger1的equals:" + (integer1.equals(newInteger1)));//true

詳情解析如下:
1返回true
無(wú)需過(guò)多解釋,基本數(shù)據(jù)類型比較值
2返回true
我們先來(lái)看下Integer ingteger1=2;這種寫(xiě)法在jdk1.5版本之前是不允許的,從1.5版本開(kāi)始java引入了自動(dòng)裝箱拆箱機(jī)制,這個(gè)賦值操作等同于:Integer integer1= Integer.valueOf(2);2中的對(duì)比也就是兩個(gè)自動(dòng)裝箱的變量進(jìn)行比較,那為什么返回的不是false?查看一下Integer的valueOf源碼:

    /**
     * Returns an {@code Integer} instance representing the specified
     * {@code int} value.  If a new {@code Integer} instance is not
     * required, this method should generally be used in preference to
     * the constructor {@link #Integer(int)}, as this method is likely
     * to yield significantly better space and time performance by
     * caching frequently requested values.
     *
     * This method will always cache values in the range -128 to 127,
     * inclusive, and may cache other values outside of this range.
     *
     * @param  i an {@code int} value.
     * @return an {@code Integer} instance representing {@code i}.
     * @since  1.5
     */
    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

可以看出,如果取值范圍在-128-127,會(huì)在數(shù)組(這個(gè)數(shù)組里面緩存了基本類型-128-127之間的Integer對(duì)象)中獲取對(duì)象,否則重新new一個(gè)Integer對(duì)象,我們2中賦值為2,所以返回true,如果大于127或小于128,則返回false。
3返回true
Integer重寫(xiě)了equals方法,兩個(gè)自動(dòng)裝箱的Integer對(duì)象,比較value。
4和5返回true 不再解釋
6返回true
基本數(shù)據(jù)類型int1和integer1自動(dòng)裝箱類型變量對(duì)比時(shí),integer1進(jìn)行自動(dòng)拆箱操作,拆箱完成,也就是兩個(gè)基本數(shù)據(jù)類型值的比較。這一步可能會(huì)有疑問(wèn),為什么是integer1拆箱而不是int1裝箱?其實(shí)這塊是java根據(jù)一個(gè)很明顯的道理進(jìn)行設(shè)計(jì)的:如果有人比較一個(gè)int類型的值和Integer類型的值,是想比較什么呢?肯定是值呀,所以這塊是對(duì)Integer對(duì)象進(jìn)行拆箱而不是對(duì)int類型裝箱了

public int intValue()  
{  
  return value;  
}

7返回true
調(diào)用equals方法的肯定是Integer對(duì)象,但是Integer類中重寫(xiě)的equals方法參數(shù)是一個(gè)Object類型呀,怎么能傳遞一個(gè)基本數(shù)據(jù)類型進(jìn)去呢?所以,這塊又是一個(gè)自動(dòng)裝箱的表現(xiàn),當(dāng)傳遞一個(gè)int類型給equals這個(gè)方法時(shí),java會(huì)自動(dòng)將這個(gè)值打包裝箱為Integer類,然后就是兩個(gè)Integer對(duì)象進(jìn)行equals判斷
8返回true
int1為一個(gè)基本類型int,newInteger1是一個(gè)Integer對(duì)象,進(jìn)行==比較的時(shí)候,會(huì)對(duì)Integer對(duì)象進(jìn)行拆箱處理,所以結(jié)果為true(等同于6)。
9返回false
自動(dòng)裝箱的integer1和newInteger1都是Integer對(duì)象(等同于4)。
10返回false
自動(dòng)裝箱的integer1和newInteger1都是Integer對(duì)象(等同于5)。

總結(jié)

對(duì)于封裝數(shù)據(jù)類型的對(duì)比,涉及自動(dòng)裝箱拆箱操作,使用需謹(jǐn)慎~~~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容