學過Java的同學或多或少都聽過自動裝箱拆箱,下邊通過代碼和字節碼文件加深下對自動拆箱裝箱的理解.
1爲什麼要有包裝類型?
作爲和基本數據類型對應的類類型存在,方便涉及到對象的操作,比如泛型必須要求我們是對象數據類型.
2 自動裝箱拆箱發生在什麼時候?
自動拆箱裝箱發生在代碼編譯期間.
通過例子來看下自動拆箱裝箱是怎麼做的:
public static void main(String[] args) {
Long a = 100L;
Long b = 100L;
long c = 100L;
Long d = new Long(100);
Long e = 1000L;
Long f = 1000L;
System.out.println(a == b);
System.out.println(a == c);
System.out.println(a == d);
System.out.println(c == d);
System.out.println(e == f);
}
下面先公佈答案:
true
true
false
true
false
蒙 a==b爲true; 爲什麼 e==f就是false? a和c是怎麼比較的? a和d又是什麼情況?
接下來我們通過字節碼文件看看到底有什麼奧祕:
public static void main(java.lang.String[]);
Code:
0: ldc2_w #2 // long 100l
3: invokestatic #4 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
6: astore_1
7: ldc2_w #2 // long 100l
10: invokestatic #4 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
13: astore_2
14: ldc2_w #2 // long 100l
17: lstore_3
18: new #5 // class java/lang/Long
21: dup
22: ldc2_w #2 // long 100l
25: invokespecial #6 // Method java/lang/Long."<init>":(J)V
28: astore 5
30: ldc2_w #7 // long 1000l
33: invokestatic #4 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
36: astore 6
38: ldc2_w #7 // long 1000l
41: invokestatic #4 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
44: astore 7
46: getstatic #9 // Field java/lang/System.out:Ljava/io/PrintStream;
49: aload_1
50: aload_2
51: if_acmpne 58
54: iconst_1
55: goto 59
58: iconst_0
59: invokevirtual #10 // Method java/io/PrintStream.println:(Z)V
62: getstatic #9 // Field java/lang/System.out:Ljava/io/PrintStream;
65: aload_1
66: invokevirtual #11 // Method java/lang/Long.longValue:()J
69: lload_3
70: lcmp
71: ifne 78
74: iconst_1
75: goto 79
78: iconst_0
79: invokevirtual #10 // Method java/io/PrintStream.println:(Z)V
82: getstatic #9 // Field java/lang/System.out:Ljava/io/PrintStream;
85: aload_1
86: aload 5
88: if_acmpne 95
91: iconst_1
92: goto 96
95: iconst_0
96: invokevirtual #10 // Method java/io/PrintStream.println:(Z)V
99: getstatic #9 // Field java/lang/System.out:Ljava/io/PrintStream;
102: lload_3
103: aload 5
105: invokevirtual #11 // Method java/lang/Long.longValue:()J
108: lcmp
109: ifne 116
112: iconst_1
113: goto 117
116: iconst_0
117: invokevirtual #10 // Method java/io/PrintStream.println:(Z)V
120: getstatic #9 // Field java/lang/System.out:Ljava/io/PrintStream;
123: aload 6
125: aload 7
127: if_acmpne 134
130: iconst_1
131: goto 135
134: iconst_0
135: invokevirtual #10 // Method java/io/PrintStream.println:(Z)V
138: return
}
鬼畫符?我們看下這些鬼到底什麼意思.
3: invokestatic #4 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
意思是執行 Long 的valueof()方法 參數爲基本類型 返回值爲Long類型
看看Long的valueof方法
public static Long valueOf(long l) {
final int offset = 128;
if (l >= -128 && l <= 127) { // will cache
return LongCache.cache[(int)l + offset];
}
return new Long(l);
}
如果傳入的基本類型在-128-127之內就,就從LongCache中取數據返回給我們.看下LongCache幹了啥
private static class LongCache {
private LongCache(){}
static final Long cache[] = new Long[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Long(i - 128);
}
}
原來如此,-128-127就直接返回給我們chche中的數據,超出這個範圍直接new一個給我們.a==b爲true,e==f爲false就說得通了.
65: aload_1
66: invokevirtual #11 // Method java/lang/Long.longValue:()J
69: lload_3
70: lcmp
拿出變量a,執行Long.longValue()返回一個基本數據類型,在和c比較.看下longValue方法
/**
* Returns the value of this {@code Long} as a
* {@code long} value.
*/
public long longValue() {
return value;
}
這部就是兩個基本數據類型比較嗎.
85: aload_1
86: aload 5
88: if_acmpne 95
取出a和d直接比較內存地址是否一樣.鐵定不一樣呀.
剩下的大家可以自己看下,有不明白的可以評論問.
3 總結
自動拆箱裝箱沒有什麼神祕的.字節碼可以告訴我們很東西.如果某些概念理解不了,試着看看字節碼文件.說不定會豁然開朗.
如果發現文章中有不妥之處,希望大家指出,共同進步.