编程语言
首页 > 编程语言> > Java终于返回了,奇怪的字节码

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

作者:互联网

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

    int i = 0;
    try {
        return i;
    } finally {
        i++;
    }

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

    StringBuffer sb = new StringBuffer("11");
    try {
        return sb;
    } finally {
        sb.append("22");
    }

它发生了奇怪的事情,它返回“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

这是我的第二个问题,为什么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

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

解决方法:

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

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

标签:java,return,jvm,java-bytecode-asm,try-catch-finally
来源: https://codeday.me/bug/20190717/1491174.html