From the program below or here, why does the last call to System.out.println(i)
print the value 7
?
class PrePostDemo {
public
Well think of it in terms of temporary variables.
i =3 ;
i ++ ; // is equivalent to: temp = i++; and so , temp = 3 and then "i" will increment and become i = 4;
System.out.println(i); // will print 4
Now,
i=3;
System.out.println(i++);
is equivalent to
temp = i++; // temp will assume value of current "i", after which "i" will increment and become i= 4
System.out.println(temp); //we're printing temp and not "i"