一 常量入栈指令
aconst_null 将null对象引用压入栈iconst_m1 将int类型常量-1压入栈iconst_0 将int类型常量0压入栈iconst_1 将int类型常量1压入栈iconst_2 将int类型常量2压入栈iconst_3 将int类型常量3压入栈iconst_4 将int类型常量4压入栈iconst_5 将int类型常量5压入栈lconst_0 将long类型常量0压入栈lconst_1 将long类型常量1压入栈fconst_0 将float类型常量0压入栈fconst_1 将float类型常量1压入栈dconst_0 将double类型常量0压入栈dconst_1 将double类型常量1压入栈bipush 将一个8位带符号整数压入栈sipush 将16位带符号整数压入栈ldc 把常量池中的项压入栈ldc_w 把常量池中的项压入栈(使用宽索引)ldc2_w 把常量池中long类型或者double类型的项压入栈(使用宽索引)
二 常量入栈常用指令集
xconst_n
|
范围
|
xconst_null
|
xconst_m1
|
xconst_0
|
xconst_1
|
xconst_2
|
xconst_3
|
xconst_4
|
xconst_5
|
iconst_n
|
[-1, 5]
|
iconst_m1
|
iconst_0
|
iconst_1
|
iconst_2
|
iconst_3
|
iconst_4
|
iconst_5
| |
lconst_n
|
0, 1
|
lconst_0
|
lconst_1
| ||||||
fconst_n
|
0, 1, 2
|
fconst_0
|
fconst_1
|
fconst_2
| |||||
dconst_n
|
0, 1
|
dconst_0
|
dconst_1
| ||||||
aconst_n
|
null、String literal、Class literal
|
aconst_null
| |||||||
bipush
|
一个字节,即 [-128, 127]
| ||||||||
sipush
|
两个字节,即 [-32768, 32767]
| ||||||||
ldc
|
四个字节,[-2^31, 2^31 - 1]
| ||||||||
ldc_w
|
宽索引
| ||||||||
ldc2_w
|
宽索引,long 或 double
|
三 常量入栈指令剖析
1 const 和 push 系列
2 ldc 系列
类型
|
常数指令
|
范围
|
int(boolean,byte,char,short)
|
iconst
|
[-1, 5]
|
bipush
|
[-128, 127]
| |
sipush
|
[-32768, 32767]
| |
ldc
|
any int value
| |
long
|
lconst
|
0, 1
|
ldc
|
any long value
| |
float
|
fconst
|
0, 1, 2
|
ldc
|
any float value
| |
double
|
dconst
|
0, 1
|
ldc
|
any double value
| |
reference
|
aconst
|
null
|
ldc
|
String literal, Class literal
|
四 实战
1 代码
// 2.常量入栈指令
public void pushConstLdc() {
int i = -1;
int a = 5;
int b = 6;
int c = 127;
int d = 128;
int e = 32767;
int f = 32768;
}
public void constLdc() {
long a1 = 1;
long a2 = 2;
float b1 = 2;
float b2 = 3;
double c1 = 1;
double c2 = 2;
Date d = null;
}
2 图解