Here is a example
public Integer add(Integer a) {
a++;
return a;
}
The corresponding bytecode instructions for this method are as follows
0 aload_1
1 astore_2
2 aload_1
3 invokevirtual #62 <java/lang/Integer.intValue : ()I>
6 iconst_1
7 iadd
8 invokestatic #52 <java/lang/Integer.valueOf : (I)Ljava/lang/Integer;>
11 astore_1
12 aload_2
13 pop
14 aload_1
15 areturn
In lines 12-13, you can observe that it performs a push onto the stack followed by an immediate pop operation.
I want to know why design it that way and why it needs another copy of "a" into LocalVariableTable
Because
a++is a shorthand notation fora = a + 1and you reassign the variable before you return it. Take a look at the following code in the bytecode: