int 赋值 1
int s = 1;
对应的字节码是:
0: iconst_1
1: istore_1
int 赋值 2
int s = 123;
对应的字节码是:
0: bipush 123
2: istore_1
int x = 123;
int y = 456;
对应的字节码是:
0: bipush 123
2: istore_1
3: sipush 456
6: istore_2
7: return
数组
int[] a = {1,2};
对应的字节码是:
0: iconst_2
1: newarray int
3: dup
4: iconst_0
5: iconst_1
6: iastore
7: dup
8: iconst_1
9: iconst_2
10: iastore
11: astore_1
int[] a = { 1, 2 };
final int size = a.length;
for (int i = 0; i < size; i++) {
int temp = a[i];
}
对应的字节码是:
0: iconst_2
1: newarray int
3: dup
4: iconst_0
5: iconst_1
6: iastore
7: dup
8: iconst_1
9: iconst_2
10: iastore
11: astore_1
12: aload_1
13: arraylength
14: istore_2
15: iconst_0
16: istore_3
17: goto 28
20: aload_1
21: iload_3
22: iaload
23: istore 4
25: iinc 3, 1
28: iload_3
29: iload_2
30: if_icmplt 20
33: return
int[] a = { 1, 2 };
for (int temp: a) {
}
对应的字节码是:
0: iconst_2
1: newarray int
3: dup
4: iconst_0
5: iconst_1
6: iastore
7: dup
8: iconst_1
9: iconst_2
10: iastore
11: astore_1
12: aload_1
13: dup
14: astore 5
16: arraylength
17: istore 4
19: iconst_0
20: istore_3
21: goto 32
24: aload 5
26: iload_3
27: iaload
28: istore_2
29: iinc 3, 1
32: iload_3
33: iload 4
35: if_icmplt 24
38: return
Switch 1
public void test(int num) {
switch (num) {
case 0:
break;
case 1:
break;
default:
break;
}
}
对应的字节码是:
0: iload_1
1: tableswitch { // 0 to 1
0: 24
1: 27
default: 30
}
24: goto 30
27: goto 30
30: return
Switch 2
public void test(int num,int num2) {
switch (num2) {
case 0:
break;
case 1:
break;
default:
break;
}
}
对应的字节码是:
0: iload_2
1: tableswitch { // 0 to 1
0: 24
1: 27
default: 30
}
24: goto 30
27: goto 30
30: return
static + switch
public static void test(int num) {
switch (num) {
case 0:
break;
case 1:
break;
default:
break;
}
}
对应的字节码是:
0: iload_0
1: tableswitch { // 0 to 1
0: 24
1: 27
default: 30
}
24: goto 30
27: goto 30
30: return