Can someone please explain why the result for z
equals 12
?
I've read about pre- and post increment but this right here still doesn't make sense for me.
public static void main(String[] args) {
int x = 1, y = 2, z = 3;
x = x + y;
y = y + x;
z = z + (x++) + (++y);
System.out.print("x = " + x + "\ny = " + y + "\nz = " + z);
}
}
Like I see it:
x = x + y
: x
-> 1
(post increment by one) + y
-> 3
(pre incremented by one), x = 4
y = y + x
: y
-> 4
(pre incremented by one again) + x
-> 2
(post incremented by one), y = 6
z = z + (x++) + (++y)
: z
-> 3 + x
-> 3
(post incremented by one) + y
-> 5
(pre incr. by one), z
= 11