Java终于返回了,奇怪的字节码

reg*_*all 3 java jvm return try-catch-finally java-bytecode-asm

我终于看到了一个Java拼图问题,并返回

    int i = 0;
    try {
        return i;
    } finally {
        i++;
    }
Run Code Online (Sandbox Code Playgroud)

什么是这个函数的返回值,我知道这将返回0,我测试另一个代码

    StringBuffer sb = new StringBuffer("11");
    try {
        return sb;
    } finally {
        sb.append("22");
    }
Run Code Online (Sandbox Code Playgroud)

这是奇怪的事情,它返回"1122" 这是我的第一个问题:为什么它返回"1122"?

我反编译这两个java代码,

    0:   iconst_0 put 0 to the stack
1:   istore_0 store the stack top into index 0
2:   iload_0  put index 0 to the stack 
3:   istore_1 store the stack top into index 1
4:   iinc    0, 1 inc the index 0 with 1
7:   iload_1 put index 1 to the stack 
8:   ireturn return the top of stack
// what's the below bytecode mean? because the index 0 has incremented, why increment again?
9:   astore_2
10:  iinc    0, 1
13:  aload_2
Run Code Online (Sandbox Code Playgroud)

这是我的第二个问题,为什么9 - 13行字节代码意味着什么?并且索引0已经递增,wy再次递增?

   0:   new     #2; //class java/lang/StringBuffer
   3:   dup
   4:   ldc     #3; //String 11
   6:   invokespecial   #4; //Method java/lang/StringBuffer."<init>":(Ljava/lang/String;)V
   9:   astore_0
   10:  aload_0
   11:  astore_1
   12:  aload_0
   13:  ldc     #5; //String 22
   15:  invokevirtual   #6; //Method java/lang/StringBuffer.append:(Ljava/lang/String;)Ljava/lang/StringBuffer;
   18:  pop
   19:  aload_1
   // like the above, this seems should return the value in the index 1, it should be "11"??
   20:  areturn
   21:  astore_2
   22:  aload_0
   23:  ldc     #5; //String 22
   25:  invokevirtual   #6; //Method java/lang/StringBuffer.append:(Ljava/lang/String;)Ljava/lang/StringBuffer;
   28:  pop
   29:  aload_2
   30:  athrow
Run Code Online (Sandbox Code Playgroud)

这是我的第三个问题,它似乎应该是索引中的值,它应该是"11"?

Daw*_*ica 6

在第一个示例中,无论finally块中发生了什么,0都会进入堆栈并返回.

在第二个示例中,对a的引用StringBuffer进入堆栈,然后StringBufferfinally块中修改自身.但是对它的引用StringBuffer没有改变,那就是返回的内容.