The three Boolean operators
! can also be used together and used multiple times to form larger Boolean expressions.
However, just like numerical operators, Boolean operators follow rules that specify the order in which they are evaluated. This order is called Boolean operator precedence.
The precedence of each Boolean operator is as follows:
! is evaluated first
&& is evaluated second
|| is evaluated third
Like numerical expressions, every expression within parentheses is evaluated first. Expressions are also read from left to right.
The following statement demonstrates how Boolean operator precedence works:
System.out.println( !(false) || true && false);
The example above will print out
true. In order, the expression is evaluated as follows:
- First, the
! Boolean operator in
true && false evaluates to
- Finally, the remaining expression
true || false evaluates to