Java Data and Operators
2. Boolean Data and Operators
2.2. Precedence and Associativity
In order to evaluate complex boolean expressions, it is necessary to understand the order in which boolean operations are carried out by the computer. For example, what is the value of the following expression?
true || | true && false |
---|
The value of this expression depends on whether we evaluate the || first or the && first. If we evaluate the || first, the expression's value will be false; if we evaluate the && first, the expression's value will be true. In the following
example, we use parentheses to force one operation to be done before the other:
EXPRESSION | EVALUATION |
---|---|
( true II true ) && false | ==> true && false => false |
true I I ( true && false ) | ==> true I I false => true |
As these evaluations show, we can use parentheses to force one operator or the other to be evaluated first. However, in Java, the && operator has higher precedence than the || operator. Therefore, the second alternative corresponds to the default
interpretation that Java would apply to the expression that has no parentheses. In other words, given the expression
true || true && false, the AND operation would be evaluated before the OR operation even though the OR operator occurs first (i.e., to the left) in the unparenthesized expression.
TABLE 5.2 Precedence order of the boolean operators | ||
Precedence Order | Operator | Operation |
1 | ( ) | Parentheses |
2 | ! | NOT |
3 | A | EXCLUSIVE-OR |
4 | && | AND |
5 | I I | OR |
Parentheses supersede
As this example illustrates, the boolean operators have a built-in precedence order which is used to determine how boolean expressions are to be evaluated (Table 5.2). A simple method for evaluating an expression is to parenthesize the expression and then evaluate it. For example, to evaluate the complex expression
true || | ! false ^ false && true |
---|
we would first parenthesize it according to the precedence rules set out in Table 5.2, which gives the following expression:
true || | ((( false) ^ false) && true) |
---|
We can then evaluate this fully parenthesized expression, step by step, starting at the innermost parentheses:
Step 1. true || | ((true ^ false) && true) |
---|---|
Step 2. true || | (true && true) |
Step 3. true || | true |
Step 4. true |
JAVA PROGRAMMING TIP Parentheses. Parentheses can (and should) be used to clarify any expression that appears ambiguous or to override Java's default precedence rules.
In addition to operator precedence, it is necessary to know about an operator's associativity in order to evaluate boolean expressions of the form (op1 || op2 || op3). Should this expression be evaluated as ((op1 || op2) || op3) or as (op1
|| (op2 || op3))? The binary boolean operators all associate from left to right. Thus, the expressions
true true true | // | Same | as: | (true ^ | true) ^ | true |
true && true && true | // | Same | as: | (true && | true) && | true |
true || true || true | // | Same | as: | (true || | true) || | true |
would be evaluated as follows:
EXPRESSION | EVALUATION |
---|---|
(true ^ true) ^ true | => false ^ true ==> true |
(true && true) && true | => true && true ==> true |
(true II true) II true | => true II true ==> true |