The update ++
makes to number
in the right-hand operand of the assignment is overwritten by assigning the result of the overall expression back to number
.
This is covered by JLS§15.26.1. Here's what happens in that assignment expression:
- The left-hand side is determined to be a variable
- The right-hand side of the assignment expression is evaluated:
- The value
80
is read from number
number
is incremented to 81 (this doesn't matter)
math.sqrt
is called with the value 80
read above
- The result of
math.sqrt
is converted to an int
- The result from #2 above (
8
) is stored in number
Note the order of Steps 2.1 and 2.2 above. That's because you've used the postfix number++
. The order would be reversed (and the result different) if you used ++number
.
The ++
on number
on the right-hand side has no effect whatsoever on the result of the expression. (In Java. In some other langauges, this would be considered Undefined Behavior allowing a compiler to do whatever it wants.)
In particular (since you mentioned expecting to get the value 9
), the ++
doesn't get applied after the result of the assignment is stored in number
. It gets applied immediately after reading the value of number
when passing it into math.sqrt
.