+ 1
Justify the output?
public class Program { public static void main(String[] args) { int x = 4; int y = x++; int w = x; int z = y++; System.out.println(y); System.out.println(w); System.out.println(z); } } output: 5 5 4 why? output should be: 4 5 4 Right?
2 Antworten
+ 3
Actually the original output is correct. You can read the expressions in this order:
int x = 4;
int y = x;
x=x+1;
int w = x;
int z = y;
y=y+1;
So, in the end you have y=5, w=5 and z=4.
+ 1
You have to check again the meanings of prefix and postfix increment operators.
For example, y = x++ will perform assignment first then do the increment.