I'm trying to get deeper with post and pre incrementors but am a bit stuck with the following expression :
public static void main(String[] args) {
int i = 0;
i = i+=(++i + (i+=2 + --i) - ++i);
// i = 0 + (++i + (i+=2 + --i) - ++i);
// i = 0 + (1 + (3 + 2) - 1 );
// i = 0 + (6 - 1 );
System.out.println(i); // Prints 0 instead of 5
}
I know I'm missing the logic somewhere but where?
What I've tried :
- Going from left to right (though I know it is not recommended)
- Going from the insidest bracket and starting from there.
Thanks for the help
PS : The comments are the details of my calculus
EDIT 1
I tried to change de hard coded value from the expression from 2
to something else and the result always gives 0
Look at this example :
int i = 0;
i = i+=(++i + (i+=32500 + --i) - ++i);
System.out.println(i); // Prints 0
This expression should logically be nowhere near 0
but somehow it does print it.
The same happens when I use a negative :
int i = 0;
i = i+=(++i + (i+=(-32650) + --i) - ++i);
System.out.println(i); // Prints 0
EDIT 2
Now, I changed the value of i
to begin with :
int i = 1;
i = i+=(++i + (i+=2 + --i) - ++i);
System.out.println(i); // Prints 2
i = 2;
i = i+=(++i + (i+=10000 + --i) - ++i);
System.out.println(i); // Prints 4
i = 3;
i = i+=(++i + (i+=(-32650) + --i) - ++i);
System.out.println(i); // Prints 6
It gives the double of i
each time, whatever the hard coded value is.
5
instead of0
– Sprint0
? Running the code on 1.6, I got result of3
. – Symbologyi +=
withi = i +
- replacing one of them (don't remember which) led to result4
, replacing the other didn't change the result. – Symbology0
: 1.4.2_19, 1.5.0_22, 1.6.0_45, 1.7.0_79, 1.8.0_51 on Windows 7, Sun/Oracle JVMs. – Trill