It's not the post-increment operator that does the trick. It's not even the lower-precedence-of-assignment-operator. It's the way
java executes statements.
I know you didn't get it. When JVM sees a high priority statement is interrupting, it memorizes the current execution, performs the high priority task and then executes the memorized execution.
Example :-
If you run the above code, you will understand that
post-increment operator is
not the one playing the trick.
Also, if you print the result returned by the method m() below, you will know that
Assignment operator has
nothing to do with it as well. Because, a
return interrupted by
finally also does show this weird behavior.
Well the reason is, the execution goes like this -
1.
return statement is memorized first. so all JVM memorizes is 'return 0;'
2. now finally is executed. The variable c's value does change actually.
3. prints updated value of c i.e. 12.
3. memorized task is executed. so, 0 is returned.
Similarly while executing c=(++a/b) in first example, what JVM does is -
1. store c=2/2.
2. increment a. so a = 3, b = 2.
3. execute c=2/2. so c = 1. a = 3, b = 2.
4. print values of a, b & c.
hope you got it.