Bug in Local variable Table construction when Usin

2019-08-13 23:49发布

I tried the following code :

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());
 }
}

The output is obviously "hi". But when I looked at the byte code using javap -v, I get the following.

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;

There is only one Local variable shown in the Table where as 3 local variables (0,1,2 check byte code load and store instructions) are being used. Is there an explanation for this?. Are local variables with value null (eventual value) being ignored?

标签: java javap
0条回答
登录 后发表回答