Integer&int,自動裝箱&自動拆箱
int 是基本資料型別它的值存在於棧中,Integer是int的包裝類它的值存在於堆中,這些都是基礎概念。
下面程式碼示例是經常被面到的簡單小問題,通過這個問題就可以知道什麼是自動裝箱什麼是自動拆箱。
package base.test;
public class Test1 {
public static void main(String[] args) {
Integer i = 10;
Integer j = 10;
System.out.println(i == j);
Integer a = 128;
Integer b = 128;
System.out.println(a == b);
int k = 10;
System.out.println(i == k);
int kk = 128;
System.out.println(a == kk);
Integer m = new Integer(10);
Integer n = new Integer(10);
System.out.println(m == n);
System.out.println(m+1 == n+1);
Integer s = 126;
Integer d = 1;
int f = 127;
System.out.println(f == (s + d));
Integer r = 126;
Integer x = 1;
Integer g = 127;
System.out.println(g == (r + x));
}
}
View Code
下邊這一段是反編譯後的程式碼。
package base.test;
import java.io.PrintStream;
public class Test1
{
public static void main(String[] args)
{
Integer i = Integer.valueOf(10);
Integer j = Integer.valueOf(10);
System.out.println(i == j);
Integer a = Integer.valueOf(128);
Integer b = Integer.valueOf(128);
System.out.println(a == b);
int k = 10;
System.out.println(i.intValue() == k);
int kk = 128;
System.out.println(a.intValue() == kk);
Integer m = new Integer(10);
Integer n = new Integer(10);
System.out.println(m == n);
System.out.println(m.intValue() + 1 == n.intValue() + 1);
Integer s = Integer.valueOf(126);
Integer d = Integer.valueOf(1);
int f = 127;
System.out.println(f == s.intValue() + d.intValue());
Integer r = Integer.valueOf(126);
Integer x = Integer.valueOf(1);
Integer g = Integer.valueOf(127);
System.out.println(g.intValue() == r.intValue() + x.intValue());
}
}
View Code
從反編譯的中可以看到兩個方法。Integer.valueOf(int);裝箱方法把基本資料型別int封裝成Integer。Integer.intValue(int);拆箱方法把Integer拆箱成int基本資料型別。
以下是valueOf的原始碼,這個原始碼簡單說如果裝箱的int大於-128並且小於127就會從IntegerCache這個類的cache取出一個Integer物件。IntegerCache是Integer的一個內部類它通過static方法給陣列cache賦值。也就是如果我們從valueOf中返回的Integer只要是在-128~127範圍內的使用的都是cache陣列中的快取物件。
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
View Code
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() {}
}
View Code
intValue(int)方法則直接返回該Integer物件對應的基本資料型別值。
public int intValue() {
return value;
}
View Code
下面我們挨個分析一段程式碼中的結果。
Integer i = 10;Integer j = 10;System.out.println(i == j);//true
兩個基本資料型別10呼叫valueOf(int)升級成包裝物件,10在cache範圍內所有返回的是同一個物件。物件使用==判斷的是堆記憶體地址所有為true。
Integer a = 128;Integer b = 128;System.out.println(a == b);//false
這個於上個的區別是128不在cache範圍內,所以返回的是兩個物件,兩個物件的堆記憶體當然是不同的。
int k = 10;System.out.println(i == k);//true
當包裝型別要和基本資料型別做運算包裝類會拆箱為基本資料型別兩個10是相等的。
int kk = 128;System.out.println(a == kk);//true
和上邊一樣,包裝型別拆箱後就是基本資料型別128兩個128是相等的。
Integer m = new Integer(10);Integer n = new Integer(10);
System.out.println(m == n);//false
因為兩個都是new出來的沒有使用valueOf(int)自然都是在堆空間中有自己的地盤,所以不是同一個物件。
System.out.println(m+1 == n+1);//true
//當包裝型別要和具體的數字運算就要拆箱兩個11是相等的。
Integer s = 126;Integer d = 1;int f = 127;System.out.println(f == (s + d));//true
兩個基本資料型別裝箱後是兩個物件,但是做加法運算就要在拆箱,127和127是相等的。
Integer r = 126;Integer x = 1;Integer g = 127;System.out.println(g == (r + x));//true
其實是把126,1,127都裝箱然後。然後r和x要運算所以拆箱,g要和他倆的結果做運算也要拆箱。