You may have already seen this somewhere, but I couldn't find a question on it. It's just a curiosity but I want to know what exactly is happening under the hood:
int j = 0;
for (int i = 0; i < 100; i++) {
j = j++;
}
System.out.println(j);
The output of above will be 0, despite first appearances. Previously, I thought it would work as follows: j
on the right (0) is assigned to j
on the left (0), then j
on the left is incremented to 1. My question is: since j
on the left is at the same address as j
on the right, why doesn't the increment stick afterwards? I had thought before that the post-incremental operator delayed execution until after the assignment. Like how this: "j = 0; k = j++;
" will result in k == 0
and j == 1
. So exactly what happens with the compiler when it's executing "j = j++;
"?