2.6 Comparing Boolean expressionsSame answer for every input

Two Boolean expressions are equivalent when they have the same value for every possible input. To prove two expressions equivalent, check every combination of inputs with a truth table. To prove they are not equivalent, you need just one counterexample: a single input where they disagree.
De Morgan's laws tell you how to push a ! inside parentheses. Negate each part, and flip the operator: !(a && b) is the same as !a || !b, and !(a || b) is the same as !a && !b. Negating a comparison flips it, too: !(x < 5) is x >= 5, and !(x == y) is x != y.
| a | b | a && b | !(a && b) | !a || !b |
|---|---|---|---|---|
| true | true | true | false | false |
| true | false | false | true | true |
| false | true | false | true | true |
| false | false | false | true | true |
?Wait, what's a truth table again?
Put the two rules together. "Not between 1 and 10," !(x >= 1 && x <= 10), becomes !(x >= 1) || !(x <= 10), which is x < 1 || x > 10. Notice that >= flipped to <, and && flipped to ||. Forgetting to flip either one is the top trap.
Comparing objects: == versus equals
For objects, == and != compare references. a == b is true only when a and b point to the very same object, which means they're aliases. The equals method compares contents: for Strings, the same characters in the same order. Two separate String objects can hold identical text, and then a == b is false while a.equals(b) is true.
1String a = "spring";2String b = new String("spring");13String c = a;4System.out.println(a == b);25System.out.println(a.equals(b));36System.out.println(a == c);47System.out.println(b != null);5
- line 2 new always makes a separate object
- line 4 Two different objects: different arrows
- line 5 Same characters, in order
- line 6 Aliases: the very same object
- line 7 == and != are the right way to test null
?Wait, what's an alias again?
There's one comparison where == is exactly right: checking for null. If a reference might be null, test it with obj == null or obj != null before calling any method on it. Calling obj.equals(...) on a null reference throws a NullPointerException.
I thought about it. Show me
- (a) De Morgan: negate each comparison and flip || to &&. !(age < 13) is age >= 13 and !(age > 19) is age <= 19. So it's age >= 13 && age <= 19.
Why?
Two flips per law: each piece, and the operator between them. - (b) It's true for ages 13 through 19: the teenagers.
- (c) Try age = 30. The original: 30 < 13 is false, 30 > 19 is true, so the || is true and the ! makes it false. The student's version: 30 >= 13 is true, so the || is true. They disagree, so they're not equivalent. (In fact the student's version is true for every age.)
Why?
One input where the two disagree is a complete proof that they are not equivalent.
Answer: (a) age >= 13 && age <= 19 (b) age is 13 to 19 (c) age = 30 makes the original false and the student's version true.
Stuck? Get a hint
- Use De Morgan: negate each comparison, and change || to &&.
- The negation of < is >=, and the negation of > is <=.
- For (c), pick an age far outside 13 to 19 and evaluate both.
- A!a || b
- B!a && b
- Ca && !b
- D!a && !b
Show the answer
B. De Morgan: negate each part and flip || to &&. !a stays !a, and !(!b) is just b, so the result is !a && b. Choice D is the trap: it forgets that negating !b gives b back. Choice A negates correctly but forgets to flip the operator.
String part = "pe"; String x = part + "ar"; String y = "pear"; System.out.println((x == y) + " " + x.equals(y));
- Atrue true
- Bfalse true
- Cfalse false
- Dtrue false
Show the answer
B. x is built from pieces at run time, so it's a new String object, separate from y. x == y compares arrows: false. x.equals(y) compares characters: both are "pear", so true. Choice A is the trap: the text is the same, but == doesn't look at the text.
- Equivalent means same value for every input. Prove with a truth table; disprove with one counterexample.
- De Morgan: !(a && b) is !a || !b; !(a || b) is !a && !b.
- Negated comparisons: !(x < y) is x >= y; !(x == y) is x != y.
- For objects, == means same object (aliases). equals compares contents. Test for null with ==.