2.5 Compound Boolean expressionsAnd, or, not, and short-circuiting

The logical operators combine boolean values. a && b (and) is true only when both a and b are true. a || b (or) is true when at least one is true, including when both are. !a (not) flips a value: !true is false.
| a | b | a && b | a || b | !a |
|---|---|---|---|---|
| true | true | true | true | false |
| true | false | false | true | false |
| false | true | false | true | true |
| false | false | false | false | true |
A range test needs two comparisons joined by &&: x >= 1 && x <= 10 is true for 1 through 10. You can't write 1 <= x <= 10 in Java; it doesn't compile. Outside a range uses ||: x < 1 || x > 10.
Precedence: ! comes first, then &&, then ||. So a || b && c means a || (b && c). Relational operators like < and == come before && and ||, so x > 0 && y > 0 needs no extra parentheses. When in doubt, add parentheses. They never hurt.
?Wait, what's precedence again?
Short-circuit evaluation
Java evaluates && and || left to right, and it stops as soon as it knows the answer. In a && b, if a is false, the whole thing must be false, so Java never evaluates b. In a || b, if a is true, the whole thing must be true, so Java never evaluates b. This is called short-circuit evaluation.
Short-circuiting is useful: put a guard on the left that protects the right. n != 0 && 100 / n > 5 never divides by zero, and s.length() > 0 && s.substring(0, 1).equals("A") never asks for a character that isn't there. Swap the two sides and the same code can crash.
1int n = 0;2String s = "";3if (n != 0 && 100 / n > 5)14{5 System.out.println("big share");6}7if (s.length() == 0 || s.substring(0, 1).equals("A"))28{9 System.out.println("empty or starts with A");10}11System.out.println(n == 0 || 100 / n > 5);3
- line 3 Left side false: the division never happens
- line 7 Left side true: substring is never called
- line 11 Same idea in a println: no exception
?Wait, why would 100 / n crash?
I thought about it. Show me
With boolean p = true, boolean q = false and int x = 6, evaluate each expression.
(a) !p || q && x > 5 (b) p || q && x < 0 (c) !(p && x % 4 == 2)
- (a) ! applies only to p, and && comes before ||: it's (!p) || (q && x > 5). !p is false. q && x > 5: q is false, so && short-circuits to false. false || false is false.
Why?
Because q is false, Java never even evaluates x > 5. - (b) It groups as p || (q && x < 0). p is true, so || short-circuits: the whole expression is true and the right side is never evaluated.
- (c) Inside the parentheses first: x % 4 is 2, so x % 4 == 2 is true. p && true is true. Then ! flips it: false.
Answer: (a) false (b) true (c) false
Stuck? Get a hint
- Add the invisible parentheses first: ! sticks to the value right after it, and && groups before ||.
- For &&, a false left side ends it. For ||, a true left side ends it.
- In (c), the parentheses force && to happen before the !.
- Astr.substring(0, 1).equals("x") && str.length() > 0
- Bstr.length() > 0 && str.substring(0, 1).equals("x")
- Cstr.length() > 0 || str.substring(0, 1).equals("x")
- Dstr.length() >= 0 && str.substring(0, 1).equals("x")
Show the answer
B. With the length check on the left, && skips the substring call whenever str is empty. Choice A has the right pieces in the wrong order: for "", substring(0, 1) runs first and throws an exception. Choice C is true for every nonempty String, and D's guard is always true, so it doesn't protect anything.
- && needs both true. || needs at least one. ! flips.
- Precedence: !, then &&, then ||. Relational operators come before all three.
- Ranges: x >= lo && x <= hi. Outside a range: x < lo || x > hi.
- Short-circuit: a false left side ends &&, a true left side ends ||. Put the guard on the left.