As many developers know, increment operator isn't atomic.
For instance:
public void incrementId(){ // id being an int field
id++;
}
Actually, this corresponds to three distinct operations:
int temp = id;
id = id + 1;
id = temp;
Besides, this method behaves similarly:
public void incrementId(){ // id being an int field
id = id + 1; // three steps also here
}
My question is:
What is the real difference behind the scene between following both operations:
id = id + 1; //three steps => non atomic
id = anotherIntVariable + 1; // one step => atomic
What concept forces the compiler to translate the first one into 3 steps and not the other?
What concept forces the compiler to translate the first one into 3 steps and not the other?
it is not, id = xyz + 1
will be compiled to the following byte code:
7 iload_2 [xyz]
8 iconst_1
9 iadd
10 istore_1 [id]
It is easy to see from the byte code that the above is not "one step"