I have this code:
class Example {
public static void main(String args[]) {
int x = 99;
if (x++ == x) {
System.out.println("x++==x : " + x);
}
if (++x == x ) {
System.out.println("++x==x : " + x); // ++x == x : 101
}
if (x == x++) {
System.out.println("x==x++ : " + x); //x==x++ : 102
}
if (x == ++x) {
System.out.println("x==++x : " + x);
}
if (++x == ++x) {
System.out.println("++x==++x : " + x);
}
if (x++ == x++) {
System.out.println("x++==x++ : " + x);
}
if (++x == x++) {
System.out.println("++x==x++ : " + x); // ++x==x++ : 109
}
}
}
and this is the output -->
++x==x : 101
x==x++ : 102
++x==x++ : 109
I want to figure out how the java compiler handles this code. I could figure out how it came up with this part of the output:
++x==x : 101
x==x++ : 102
But I couldn't with this part of the output:
++x==x++ : 109
How does this code work? And most importantly how does the last output work?