我尝试了以下代码:
class testingFinally {
public static String getMessage() {
String s = "hi";
try {
return s;
} finally {
s = null;
}
}
public static void main(String a[]) {
System.out.println(getMessage());
}
}
输出显然是 "hi"
。但是当我使用javap -v
查看字节代码时,我得到以下内容。
public static java.lang.String getMessage();
descriptor: ()Ljava/lang/String;
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=1, locals=3, args_size=0
0: ldc #16 // String hi
2: astore_0
3: aload_0
4: astore_2
5: aconst_null
6: astore_0
7: aload_2
8: areturn
9: astore_1
10: aconst_null
11: astore_0
12: aload_1
13: athrow
LocalVariableTable:
Start Length Slot Name Signature
0 10 0 s Ljava/lang/String;
表中只显示了一个局部变量,其中使用了3个局部变量(0,1,2检查字节代码load
和store
指令) 。对此有解释吗? 局部变量是否忽略值null(最终值)?