Java Post增量与OpCode IINC
我们大多数人都熟悉邮政增量,但是当我查看字节码指令时,它表明了增量发生在调用该方法之前。
示例:
int x = 0;
aMethod(x++);
bytecode:
0: iconst_0
1: istore_1
2: iload_1
3: iinc 1, 1
6: invokestatic #2 // Method aMethod:(I)V
9: return
有人可以澄清一下吗?
Most of us are familiar with post increment, but when I look to the bytecode instructions, it shows the increment happened before the invocation to the method.
Example:
int x = 0;
aMethod(x++);
bytecode:
0: iconst_0
1: istore_1
2: iload_1
3: iinc 1, 1
6: invokestatic #2 // Method aMethod:(I)V
9: return
Can someone clarify this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
多亏了@VOO,
我为比较目的提取了另一个示例:
在指令2#和5#中可以看到差异(在ILOAD_1之前调用IINC),而在In Post Incement中,ILOAD_1在IINC之前调用。
这意味着增量将发生在变量本身上,而不是将其推向堆栈的副本上,该副本被用作调用该方法的参数。
来自在这里:
Thanks to @voo
I've made a another example for pre increment for comparison purpose:
The difference can be seen in instruction 2# and 5# (iinc invoked before iload_1), while in in post increment, iload_1 invoked before iinc.
That means the increment will occur on the variable itself and not on the copy pushed onto the stack, which been used as parameter to invoke the method.
from here: