can anybody explain this why its happening
int i=0;
i=i++;
i=i++;
i=i++;
System.out.println(i);
it prints zero.
i++
is a postincrement (JLS 15.14.2). It increments i
, but the result of the expression is the value of i
before the increment. Assigning this value back to i
in effect keeps the value of i
unchanged.
Break it down like this:
int i = 0;
int j = i++;
It's easy to see why j == 0
in this case. Now, instead of j
, we substitute the left hand side with i
. The right hand side value is still 0
, and that's why you get i == 0
in your snippet.