This is due to the priority of the operator, that is, the order in which operators are evaluated by the Java interpreter.
See here for documents. One useful acronym is BUDMASRELCA - B , Unary, D iv- M Multiplication (actually multiplicative includes modulo), A ddittion- S ubtraction, R , E , L strong> ogical, onditional (triple), And ssignment. I skipped the bitwise operators, but they can be grouped according to logic, and they take precedence over normal logical operators.
0% 2! = 0 is first evaluated as 0% 2 (multiplicative), and then the result 0 is evaluated using! = 0. (equality)
Internally, compilers build a binary expression tree to represent the order as shown below for your case, using operators as roots and leaving them as values ββor further operators (in the recursive case). Thus, subtrees that have statements must be evaluated before the root statement can be evaluated with the value that it leaves.
!= / \ % 0 /\ 0 2
source share