Integer和int的面試題!

題記:當研究完之後突然發現自己用了這麼久的Integer和int居然連這點都不知道,記錄一下自己的成長。

一、總結一下Java的基本類型和引用類型

 

特點總結:

    1》引用數據類型的大小統一爲4個字節,記錄的是其引用對象的地址!

           字節:字節(Byte )是計算機信息技術用於計量存儲容量的一種計量單位,作爲一個單位來處理的一個二進制數字串,是構成信息的一個小單位。最常用的字節是八位的字節,即它包含八位的二進制數。1和0是計算機儲存的基本單位,包括現在電腦上看到的所有一切都是由1和0兩個數組成的,一個即是一個位,8位(bit)一個字節,我們在電腦中看到的圖像視頻等都是計算機通過對儲存器中無數個1和0的計算得來的。

    2》基本數據類型

     一個字節等於8位:1byte=8bit.

數據類型 字節 字節大小(位) 封裝類型 取值範圍
byte 1byte 8bit Byte

最小值是 -128(-2^7);

最大值是 127(2^7-1);

short 2byte 16bit Short

最小值是 -32768(-2^15);

最大值是 32767(2^15 - 1);

int 4byte 32bit Integer

最小值是 -2,147,483,648(-2^31);

最大值是 2,147,483,647(2^31 - 1);

long 8byte 64bit Long

最小值是 -9,223,372,036,854,775,808(-2^63);

最大值是 9,223,372,036,854,775,807(2^63 -1);

float 4byte 32bit Float

單精度浮點數字長32位,尾數長度23,指數長度8,指數偏移量127;

-3.403E38~3.403E38

double 8byte 64bit Doble

雙精度浮點數字長64位,尾數長度52,指數長度11,指數偏移量1023;

-1.798E308~1.798E308

char 2byte 16bit Character

最小值:Character.MIN_LOW_SURROGATE

最大值:Character.MAX_HIGH_SURROGATE

boolean 1byte   Boolean true,false


十進制整數,如:99, -500, 0     3》Java 語言整型常量的四種表示形式

  • 八進制整數,要求以 0 開頭,如:015

  • 十六進制數,要求 0x 或 0X 開頭,如:0x15

  • 二進制數,要求0b或0B開頭,如:0b01110011

二、拆箱,裝箱

簡單來說:

自動裝箱就是自動將基本數據類型轉換爲包裝器類型;

自動拆箱就是自動將包裝器類型轉換爲基本數據類型。

三、int和Integer的區別

1、Integer是int的包裝類,int則是java的一種基本數據類型 
2、Integer變量必須實例化後才能使用,而int變量不需要 
3、Integer實際是對象的引用,當new一個Integer時,實際上是生成一個指針指向此對象;而int則是直接存儲數據值 
4、Integer的默認值是null,int的默認值是0

四、代碼比較

1,new兩個Integer進行比較

    @Test
    public void test2(){
        Integer aa=new Integer(1);
        Integer bb=new Integer(1);
        System.out.println(aa==bb);//false
        System.out.println(aa.equals(bb));//true
    }

總結:這裏不用多說,aa==bb比較的堆內存中的地址,因爲new了兩個對象所以爲false。

aa.equals(bb)比較的是文本內容所以未ture。

2,new一個Integer和一個int類型的比較

    @Test
    public void test2(){
        Integer aa=new Integer(1);
        int bb=1;
        System.out.println(aa==bb);//true
        System.out.println(aa.equals(bb));//true
    }

總結:按照常理一個是堆內存中的一個是常量池的,肯定是false,true纔對啊,爲什麼是true,true。原因:因爲包裝類Integer和基本數據類型int比較時,java會自動拆包裝爲int,然後進行比較,實際上就變爲兩個int變量的比較)

3,new一個Integer,直接賦值一個Integer

    @Test
    public void test2(){
        Integer aa=new Integer(1);
        Integer bb=1;
        System.out.println(aa==bb);//false
        System.out.println(aa.equals(bb));//true
    }

總結:因爲非new生成的Integer變量指向的是java常量池中的對象,而new Integer()生成的變量指向堆中新建的對象,兩者在內存中的地址不同

4,兩個Integer直接賦值

    @Test
    public void test2(){
        Integer aa=1;
        Integer bb=1;
        System.out.println(aa==bb);//true
        System.out.println(aa.equals(bb));//true
    }

總結:這裏不難理解,全部指向常量池中,所以全是true。

但是當值大於127時看下邊代碼

    @Test
    public void test2(){
        Integer aa=128;
        Integer bb=128;
        System.out.println(aa==bb);//false
        System.out.println(aa.equals(bb));//true
    }

總結:是不是膛目結舌,不由自主的一句WC,原因得從源碼說起:看一下Integer的源碼

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

當i大於等於最小值-128並且小於等於最大值127時,會調用IntegerCache.cache(),進行緩存,所以第一個兩個true是因爲第一次添加到了緩存中,第二次獲取的也是緩存中的數值。所以打印了兩個true。

當大於127時,代碼底層實際上是new 了兩個Integer所以其實是再對內存中存在兩個不同的對象。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章