I'm having a program that prints the value of an integer number x
after a post-increment operation. I want to know what actually happened during that operation?
The post-increment operator first assign the value then increment it but this didn't happen in the first example, I don't know if this because it is assigned to the same variable x
so the memory will not be affected twice on the same variable in one operation!
int x = 1; //x = 1
x = x++; //x = 1
System.out.println(x); //x = 1
int x = 1; //x = 1
int y = 1; //y = 1
y = x++; //y = 1, x = 2
System.out.println(x); //x = 2
I expect the output of x
to be 2
, but the actual output is 1