考虑非常简单的人为示例代码:
public class TestJavap {
public static void main(String[] args) {
int a = 3;
int b = 7;
}
}
javap 产生这个:
public static void main(java.lang.String[]);
Code:
0: iconst_3
1: istore_1
2: bipush 7
4: istore_2
5: return
为什么编译器会为非常相似的字段
a
和b
. 两者都是用常量字面量初始化的整数类型。因为
a
它通过从池中获取常量iconst_3
,然后将其存储在变量中,istore_1
而对于 b 它使用完全不同的机制(bipush
和 的组合istore
)。