I know how pre and post increment operators work, but recently I found out a strange behavior in Java. What i knew so far was (as explained in this answer as well):
a = 5;
i=++a + ++a + a++; =>
i=6 + 7 + 7; (a=8)
which clearly shows that the ++a returns the value after increment, a++ returns the value before increment. But very recently, I came across this piece of code:
int a = 0;
int b = a++;
int c = ++b;
System.out.println("b=" + b + "\nc=" + c);
The output to this piece of code is:
b=1
c=1
which is strange as a++ should return 0, and increment a, keeping the value of b to 0, as evident from the answer quoted above, while in the next line, b should be incremented first before the value is assigned to c, making it 1. What is happening here?