I have this piece of code in JAVA 8:
int x=3;
int y = --x + x++ + --x ;
System.out.print("y: " + y + " x: "+x);
As I understand it should be split like this:
And it prints y: 6 x: 2
What is the order of operation in here?
It's because of the way pre
and post
increments work, it gets evaluated like this:
1. y = --x + x++ + --x ;
2. y = 2 + (2)++ + --(3);
3. y = 2 + 2 + 2;
4. y = 6
After 2 decrements and 1 increment, x
becomes 2.