The code looks like this:
public class TestPlusPlus{ public static void main(String[] args){ int k = addAfterReturn(10); System.out.println(k); //Output 10 int k1 = addbeforeReturn(10); System.out.println(k1); //Output 11 } public static int addbeforeReturn(int i){ return ++i; } public static int addAfterReturn(int i){ return i++; }}From the bytecode level,
The bytecode of addAfterReturn is as follows:
0: iload_01: iinc 0, 14: ireturn
It's very simple, iload_0 means putting the value of the element indexed to 0 in the local variable table to the top of the stack. Therefore, the value of i passed in is placed on the top of the stack, and iinc 0,1 means to place the local variable table
Elements with index 0 are added to 1, and finally ireturn puts the int value at the top of the stack into the caller's stack frame. (The stacks here are operand stacks)
Therefore, iinc 0 and 1 do not actually affect the returned value, so the return is still 10.
Similarly, addBeforeReturn bytecode is as follows:
0: iinc 0, 13: iload_04: ireturn
I will leave it to everyone for their own analysis. In fact, it is obvious that this is incrementing first and then putting it into the stack. So the returned value is actually incremented.
Now let’s do an interview question that I have corrected myself.
public class TestPlusPlus2{ static{ x=5; int y = 10; } static int x,y; public static void main(String args[]){ x--; myMethod( ); System.out.println(x+y+ ++x); } public static void myMethod( ){ y=x++ + ++x; }}Do you know the answer? Run it, the correct answer is 23.
If you really understand the above, this question is also very simple. First of all, you know the values of x and y before executing main. This involves the class loading mechanism. The preparation stage of class loading will assign the static variable to the zero value of this type. If int is int, it corresponds to 0. In the initialization stage of the class, the static blocks and static variable assignment behaviors in the class are collected according to the code order to generate the method. Therefore, in this test, x = 5, y = 0;
Continue to analyze, x--' ,所以x = 4,y = 0, myMethod 中y = x++ + ++x;
x++ will not add 1 before executing the subsequent addition operation, just like return i++ only after return (you can understand the return as the iload stack operation above, not the iretrun instruction), so the first add is 4, but it should be noted here that before executing ++x, incrementing has already taken effect on the actual x, that is, x is already 5, so the second adder is ++i and 6, so y=10.
Then it's very simple x+y+++x is 6+10+7 is 23. Fortunately, if you can understand the bytecode, you can translate the bytecode of the code and use the bytecode to verify it, and you can also find such results.
Let’s take a look at another example, which was provided by the big guys in the group. I think it is also very basic.
public static int plus(int i){ try{ ++i; System.out.println("try"); return i++; } finally{ System.out.println("finally"); i++; //return i; }}public static void main(String[] args){ System.out.println(Test1.plus(5));}Can you think of how much is the final output? If you remove the comments, this is why you don't return in the finally block. Because no matter whether it is right or wrong, a certain value may always be returned.
Summarize
The above is a detailed explanation of i++ and ++i in java interview questions introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!