There is such an interview question:
INT A = 2, B = 3, C = 1 A = - B C; C- = B A ; System.out.Println ("A =" A ", B =" B ", c = " C); What is the output of the output?
The correct answer is: a = 6, b = 2, c = -6
Now resolve the specific operational process of this program
Execute Javap -c Test to get the execution code of this program is as follows:
0 iconst_21 iStore_12 ICONST_33 ISTORE_24 ICONST_15 ISTORE_3
6 ILOAD_17 IINC 2, -110 iLoad_211 iLoad_312 Iadd13 Iadd14 iStore_1
15 ILOAD_316 ILOAD_217 iLoad_118 IINC 1,121 Iadd22 ISUB23 ISTORE_3
From 0 to 5 are the first statement, the initialized part first iConst_2 will put a integer constant 2PUSH to OPERAND Stack and IStore_1 will use the 2Store to Local VariableIableInDex in the Operand Stack to 1, at which time the variable A is initialized, the other two Variables are similar, the INDEXs of variable B and variable C are 2 and 3, respectively, and the following operations use their index
The operation of the second statement at the beginning of the 6th sentence actually saves the value of the final variable in the Local Variable, and the value of the variable is operated in the Operand Stack. iLoad_1 Put the variable A from Local Variable to Operand Stack, Chapter 7 IINC 2, -1 performs an increase in -1 to the local variable variable B, in Java, the -x actually executes (- x) such The operation then stores the variables B and C to the Operand Stack, at which time A = 2, b = 2, c = 1IAdd instruction in Stack, and then performs the addition operation, and The result of the operation result PUST Stack, then A = 2 and the intermediate value 3 (b c) in Stack are left, b = 2 and c = 1 have been out of the stack, and then execute IADD, a out of the stack, the stack is left Intermediate value 5 (2 3), iStore_1 instructions use the value of the value in Operand Stack 5 into the local variable of the variable A. After the end of this round, a = 5, b = 2, c = 1
The operation of the third statement at the beginning of the fifty sentences PUSH to the OPERAND Stack in the third statement first, then execute IINC 1, 1 to a Local Variable plus 1, at which time A = 6 and then execute IADD, adding the top two (A = 5, b = 2) in the Operand Stack, at this time, although A in Local Variable is already 6, but the A in Stack is still 5, a and b are Pop There is a C = 1 and the intermediate value 7 in the OPERAND Stack, and then ISUB performs a subtraction operation on both values in Stack, and the remaining value of STACK is stored in the Stack of -6 in C. Local Variable.
The final result A = 6, b = 2, c = -6
Summary: In Java, it is the priority of the operator, so and - this high priority is the first to calculate. It is more interesting to put the order of the inrest, the first sentence is first Local variable execution subtraction push to stack, and in the second sentence, the value of the value PUSH to STACK will then perform the addition. This involves the definition of the expression in Java, according to JVM SPEC, any arithmetic operation is placed in the Operand Stack in Operand Stack. For the entire expression, the value of -b is first calculated, and for A , the value of the expression is actually A, so the value of a value PUSH Stack will be executed in . Hurrying in a hurry, I don't know if there is an omissiture or wrong. ^ _ ^