When i run the following example i get the output 0,2,1
class ZiggyTest2{
static int f1(int i) {
System.out.print(i + \",\");
Hope this explaination might be of some help :
j = i++; // Here since i is post incremented, so first i being 0 is assigned to j
// and after that assignment , i is incremented by 1 so i = 1 and j = 0.
i = i++ + f1(i); // here again since i is post incremented, it means, the initial value
// of i i.e. 1 as in step shown above is used first to solve the
// expression i = i(which is 1) + f1(1)**Since i is 1**
// after this step the value of i is incremented. so i now becomes 2
// which gets displayed in your last System.out.println(i) statement.
Do try this
i = ++i + f1(i); // here i will be first inremented and then that value will be used
// to solve the expression i = i + f1(i)'
So in short during post increment, expression is first solved and then value is incremented. But in pre increment, value is first incremented and then expression is solved.
But if you write only
i++;
++i;
Then both means the same thing.
Regards