Often there is such a problem:
int j;
for(int i = 0; i < 1000; i++){
j = j ++;
}
System.out.println (j); // Output 0
Why no matter how many times the loop, j are zero?
There are two reasons:
1. For j ++, java caching mechanism uses an intermediate, referred to the first cache j
2. The "+" operator, a higher priority than "="
Based on the above, j = j ++ implementation process can be broken down as follows:
1. java intermediate variables, temporary value of j: int tmp = j;
2. "=" j do the right increment: j = j + 1, j to 1 at this time
3. Since the increase is completed, the "=" assignment operator, note that this time the assignment is assigned tmp: j = tmp, tmp = 0 and so, ultimately j = 0
Question: If you are using j = ++ j how?
The answer would be 100, because the results of the first increment ++ j will be assigned to the intermediate variables, assuming j = 0, j = ++ j can be broken down as follows:
1. j = j + 1; // j = 1 case
2. int tmp = j; // tmp = 1 case
3. j = tmp; // j = 1 case