Java基礎之int和Integer有什麼區別

1 int與Integer的基本使用對比

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

2 int與Integer的深刻對比

(1)因爲Integer變量其實是對一個Integer對象的引用,因此兩個經過new生成的Integer變量永遠是不相等的(由於new生成的是兩個對象,其內存地址不一樣)。java

Integer i = new Integer(100);
Integer j = new Integer(100);
System.out.print(i == j); //false

(2)Integer變量和int變量比較時,只要兩個變量的值是向等的,則結果爲true(由於包裝類Integer和基本數據類型int比較時,java會自動拆包裝爲int,而後進行比較,實際上就變爲兩個int變量的比較)編程

Integer i = new Integer(100);
int j = 100;
System.out.print(i == j); //true

(3)非new生成的Integer變量和new Integer()生成的變量比較時,結果爲false。(由於非new生成的Integer變量指向的是java常量池中的對象,而new Integer()生成的變量指向堆中新建的對象,二者在內存中的地址不一樣)數組

Integer i = new Integer(100);
Integer j = 100;
System.out.print(i == j); //false

(4)對於兩個非new生成的Integer對象,進行比較時,若是兩個變量的值在區間-128到127之間,則比較結果爲true,若是兩個變量的值不在此區間,則比較結果爲false緩存

 

  對於第4條的緣由: java在編譯Integer i = 100 ;時,會翻譯成爲Integer i = Integer.valueOf(100)。而java API中對Integer類型的valueOf的定義以下,對於-128到127之間的數,會進行緩存,Integer i = 127時,會將127進行緩存,下次再寫Integer j = 127時,就會直接從緩存中取,就不會new了。app

public static Integer valueOf(int i){
    assert IntegerCache.high >= 127;
    if (i >= IntegerCache.low && i <= IntegerCache.high){
        return IntegerCache.cache[i + (-IntegerCache.low)];
    }
    return new Integer(i);
}

3 Java兩種數據類型

3.1 Java兩種數據類型分類

(1)原始數據類型,分爲boolean、byte、int、char、long、short、double、float; 
(2)引用數據類型 ,分爲數組、類、接口。spa

3.2 Java爲每一個原始類型提供了封裝類

  爲了編程的方便仍是引入了基本數據類型,可是爲了可以將這些基本數據類型當成對象操做,Java爲每 一個基本數據類型都引入了對應的包裝類型(wrapper class),int的包裝類就是Integer,從Java 5開始引入了自動裝箱/拆箱機制,使得兩者能夠相互轉換。.net

原始類型: boolean,char,byte,short,int,long,float,double
封裝類類型:Boolean,Character,Byte,Short,Integer,Long,Float,Double

4 基本解析

4.1 自動裝箱:將基本數據類型從新轉化爲對象

public class Test {  
        public static void main(String[] args) {  
            //聲明一個Integer對象
            Integer num = 9;

            //以上的聲明就是用到了自動的裝箱:解析爲:Integer num = new Integer(9);
        }  
    }

  9是屬於基本數據類型的,原則上它是不能直接賦值給一個對象Integer的,但jdk1.5後你就能夠進行這樣的聲明。自動將基本數據類型轉化爲對應的封裝類型,成爲一個對象之後就能夠調用對象所聲明的全部的方法。翻譯

4.2 自動拆箱:將對象從新轉化爲基本數據類型

public class Test {  
        public static void main(String[] args) {  
            //聲明一個Integer對象
            Integer num = 9;

            //進行計算時隱含的有自動拆箱
            System.out.print(num--);
        }  
    }

  由於對象時不能直接進行運算的,而是要轉化爲基本數據類型後才能進行加減乘除。對比:設計

/裝箱
Integer num = 10;
//拆箱
int num1 = num;

5 深刻解析

5.1 狀況描述

public class Test {  
        public static void main(String[] args) {  
            //在-128~127 以外的數
            Integer num1 = 128;   Integer num2 = 128;           
            System.out.println(num1==num2);   //false

            // 在-128~127 以內的數 
            Integer num3 = 9;   Integer num4 = 9;   
            System.out.println(num3==num4);   //true
        }  
    }

  解析緣由:歸結於java對於Integer與int的自動裝箱與拆箱的設計,是一種模式:叫享元模式(flyweight)。 
  加大對簡單數字的重利用,Java定義在自動裝箱時對於值從–128到127之間的值,它們被裝箱爲Integer對象後,會存在內存中被重用,始終只存在一個對象。 
而若是超過了從–128到127之間的值,被裝箱後的Integer對象並不會被重用,即至關於每次裝箱時都新建一個 Integer對象。

5.2 Integer源碼解析

  給一個Integer對象賦一個int值的時候,會調用Integer類的靜態方法valueOf,源碼以下:

public static Integer valueOf(int i) {
        assert IntegerCache.high >= 127;
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

  IntegerCache是Integer的內部類,源碼以下:

/**
      * 緩存支持自動裝箱的對象標識語義
      * -128和127(含)。
      *
      * 緩存在第一次使用時初始化。 緩存的大小
      * 能夠由-XX:AutoBoxCacheMax = <size>選項控制。
      * 在VM初始化期間,java.lang.Integer.IntegerCache.high屬性
      * 能夠設置並保存在私有系統屬性中
     */
    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) -1);
            }
            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() {}
    }

6 參考連接

java 自動裝箱與拆箱

int和Integer有什麼區別?

相關文章
相關標籤/搜索