int i=10;
i=i++;//assigning a value is done after the expression is evaluated... here the expression value is "10 " since u r using post increment. after the expression evaluation is complete "i" value is 11. But as u have used the post increment the value of expression will be 10 which is assigned and printed.
System.out.println(i); Why 10?
And:
int i=10;
i=i++ + i++;
System.out.println(i); Why 21?
the expression here ((i++) + (i++)). We know that expression evaluation takes place from left to right.so at first (i++) it results in 10 and the second (i++) results to 11. so by adding up u will result in 21.
j++ uses the current value of j as the value of the expression first, then adds 1 to j. It is equivalent to the following statements:
result = j;
j += 1;
return result;
so it returns 10 which is assigned to i again.
same with the case of (i++)+(i++)
I dont know if it is a bug , but this is the only plausible solution
Think of it this way: if a ++ in front of/behind a field makes a copy of the field. The copy is the current field value without adding 1 if ++ is behind / with adding 1 if ++ is in front of the field.
i=i++is the same asi=i = 10
same with this example:
int i=10;
int j = 10;
i=i++ + j++;
i =i++ + j++is the same asi=i + j = 10+10 = 20
for the special case
int i=10;
i=i++ + i++;
you need to keep in mind that the first i++ copies the current value of i which is 10. The second i++ is also copying the current value of i but which is already 11 because of the first i++. Example:
i = i++ + i++
i = a + i++; //a is a copy of i that is 10, i itself is now 11
i = a + b; //b is a copy of i that is 11, i itself is now 12
i = 10 + 11 = 21
int i=10;
int j =i++ + i++;
System.out.println(i);
System.out.println(j);