class FinalTest{
void a(){
final int i=10;
int j=10;
}
}
stack=2, locals=3, args_size=1
// 这个地方,应该有i的赋值?
0: bipush 10
2: istore_2
3: return
class FinalTest{
void a(){
int i=10;
int j=10;
}
}
stack=2, locals=3, args_size=1
0: bipush 10
2: istore_1
3: bipush 10
5: istore_2
6: return
--------------------------------------------------------------------------------------------
class FinalTest{
void a(){
int i=10;
int j=10;
b(i);
b(j);
}
void b(int i){
}
}
stack=2, locals=3, args_size=1
0: bipush 10
2: istore_1
3: bipush 10
5: istore_2
6: aload_0
7: iload_1
8: invokevirtual #2 // Method b:(I)V
11: aload_0
12: iload_2
13: invokevirtual #2 // Method b:(I)V
16: return
class FinalTest{
void a(){
final int i=10;
int j=10;
b(i);
b(j);
}
void b(int i){
}
}
stack=2, locals=3, args_size=1
0: bipush 10
2: istore_2
3: aload_0
// 直接将10入操作数栈,而不在通过i
4: bipush 10
6: invokevirtual #2 // Method b:(I)V
9: aload_0
10: iload_2
11: invokevirtual #2 // Method b:(I)V
14: return
--------------------------------------------------------------------------------------------
// object加不加final一个样
class FinalTest{
void a(){
final Object o=new Object();
Object o1=new Object();
}
}
class FinalTest{
void a(){
Object o=new Object();
Object o1=new Object();
}
}
0: new #2 // class java/lang/Object
3: dup
4: invokespecial #1 // Method java/lang/Object."<init>":()V
7: astore_1
8: new #2 // class java/lang/Object
11: dup
12: invokespecial #1 // Method java/lang/Object."<init>":()V
15: astore_2
16: return
--------------------------------------------------------------------------------------------
// 支持字面量的Object类型--String
class FinalTest{
void a(){
final String s1="a";
String s2="a";
}
}
// 同int一样
0: ldc #2 // String a
2: astore_2
3: return
class FinalTest{
void a(){
String s1="a";
String s2="a";
}
}
0: ldc #2 // String a
2: astore_1
3: ldc #2 // String a
5: astore_2
6: return
// 最后,如果有调用的情况,应该和INT型的处理是一样的
jdk 编译器 对final字段的处理
原文:http://www.cnblogs.com/yanbo2016/p/5293026.html