You can see this problem accidentally:
int a = 1;a = a++;
A = ?, I tested it myself. In vs2008, I Got a = 2. in linux, the gcc compilation result is also 2. in java, result a = 1 in the JDK7 environment;
In order to find out what is going on, we did some research as follows:
The following assembly code is available in vs2008:
a = a++;00881445 8B 45 F8 mov eax,dword ptr [a] 00881448 89 45 F8 mov dword ptr [a],eax 0088144B 8B 4D F8 mov ecx,dword ptr [a] 0088144E 83 C1 01 add ecx,1 00881451 89 4D F8 mov dword ptr [a],ecx
It's clear! That is, it is decomposed into two sentences: a = a; a ++; so the result is a = 2. It is natural. Next, let's take a look at how the java language handles the problem:
L0 LINENUMBER 4 L0 ICONST_1 ISTORE 1 L1 LINENUMBER 5 L1 ILOAD 1 IINC 1 1 ISTORE 1 L2
Let's just say: After the java language is compiled, it runs in the java Virtual Machine. The value transfer of the java Virtual Machine is based on the stack and does not use registers. The above is its bytecode, which can be clearly seen
Working principle: some people may not understand it. Let me explain it:
The code between L0 and L1 means: ICONST_1: Press constant 1 into the stack, ISTORE 1: assigns the element at the top of the stack (that is, constant 1) to variable, between L1 and L2: ILOAD 1: press the value of a into the stack, IINC 1 1:
Let the value of a in the constant pool increase from 1. In this case, a = 2 and ISTORE 1 in the constant pool: The top element of the stack (1) is assigned to a in the constant pool, so a = 1. I do not know how the Java Virtual Machine can handle this problem.
The value is 1 instead of 2.