Lesson 2.6: Comparing Boolean Expressions
Lesson 2.6: Comparing Boolean Expressions
Key Vocabulary
| Term | Definition |
|---|---|
| equivalent expressions | Two boolean expressions that evaluate to the same value for every possible combination of input values. (EK 2.6.A.1) |
| truth table | A table showing the output of a boolean expression for all possible combinations of input values. Used to prove or disprove equivalence. (EK 2.6.A.1) |
| De Morgan's Law | A pair of transformation rules: !(a && b) ≡ !a || !b and !(a || b) ≡ !a && !b. When NOT is distributed, AND becomes OR and OR becomes AND. (EK 2.6.A.2) |
| object reference | A variable that stores a memory address pointing to an object. Two references can point to the same object or different objects with identical content. (EK 2.6.B) |
| .equals() | A method that compares object content rather than memory addresses. The correct way to compare String values. Returns a boolean. |
| null | A special value indicating a reference variable does not point to any object. Calling a method on a null reference throws a NullPointerException at runtime. |
Equivalent Boolean Expressions (EK 2.6.A.1)
Two boolean expressions are equivalent if and only if they produce the same result for every possible combination of input values — not just the inputs you happen to test. Proving equivalence requires checking all cases, which is exactly what a truth table does. Finding a single case where two expressions differ is sufficient to prove they are NOT equivalent.
CED Connection (EK 2.6.A.1)
"Two Boolean expressions are equivalent if they evaluate to the same value in all cases. Truth tables can be used to prove Boolean expressions are equivalent." The AP exam tests this with code questions asking which expression is equivalent to a given one, and with truth table fill-in questions.
Worked Example — Proving Equivalence with Truth Table
Are a > b and !(a <= b) equivalent?
| a | b | a > b | a <= b | !(a <= b) | Match? |
|---|---|---|---|---|---|
| 3 | 5 | false | true | false | ✓ |
| 5 | 5 | false | true | false | ✓ |
| 7 | 5 | true | false | true | ✓ |
All cases match → a > b and !(a <= b) are equivalent. This makes sense logically: "greater than" is exactly the negation of "less than or equal to."
De Morgan's Law (EK 2.6.A.2)
De Morgan's Law is one of the highest-frequency topics on the AP CSA exam. It provides two transformation rules for distributing NOT across AND and OR. The CED states both rules explicitly:
De Morgan's Law — Both Rules
!(a && b) is equivalent to !a || !b // AND becomes OR !(a || b) is equivalent to !a && !b // OR becomes AND
Memory rule: push NOT inside the parentheses, flip every operand, and flip the operator (AND ↔ OR).
Worked Example — Proving De Morgan's with Truth Table
Verify !(a && b) ≡ !a || !b:
| a | b | a && b | !(a && b) | !a || !b | Match? |
|---|---|---|---|---|---|
| T | T | T | F | F | ✓ |
| T | F | F | T | T | ✓ |
| F | T | F | T | T | ✓ |
| F | F | F | T | T | ✓ |
All four rows match → the equivalence is proven.
Worked Example — Applying De Morgan's in Code
// Original condition: not (raining AND cold)
if (!(isRaining && isCold)) { /* go outside */ }
// De Morgan's applied: !AND becomes OR with flipped operands
if (!isRaining || !isCold) { /* go outside */ } // equivalent
// Second law: not (hungry OR tired)
if (!(isHungry || isTired)) { /* keep working */ }
// De Morgan's applied: !OR becomes AND with flipped operands
if (!isHungry && !isTired) { /* keep working */ } // equivalent
De Morgan's lets you rewrite complex negations into forms that are easier to read and reason about. On AP FRQs, either form is accepted as correct when both are equivalent.
AP Trap: Forgetting to Flip the Operator
The single most common De Morgan's mistake: !(a && b) becomes !a && !b. WRONG — the AND must become OR. Correct: !a || !b. Similarly !(a || b) becomes !a || !b is WRONG — the OR must become AND. Correct: !a && !b. The operator always flips when you distribute NOT.
Comparing Object References (EK 2.6.B)
With reference types (objects), == and != compare memory addresses — whether two variables point to the same object in memory, not whether their content is identical. A reference variable can also hold the special value null, meaning it does not point to any object. Calling a method on a null reference throws a NullPointerException at runtime.
Worked Example — == vs. .equals() on Strings
String a = new String("hello");
String b = new String("hello");
String c = a;
String d = null;
System.out.println(a == b); // false — different objects
System.out.println(a == c); // true — same object (c points to a)
System.out.println(a.equals(b)); // true — same content
System.out.println(a.equals(c)); // true — same content
System.out.println(d == null); // true — d points to nothing
System.out.println(d != null); // false
// Safe null check before calling .equals():
if (a != null && a.equals(b)) { // short-circuits if a is null
System.out.println("Match");
}
| Method / Operator | What it compares |
|---|---|
| == (objects) | Memory addresses. true only if both variables point to the exact same object.⚠ Use only for null checks: obj == null
|
| .equals() (objects) | Object content. true if the objects have equal content, even if stored at different addresses.⚠ Always null-check before calling: if (s != null && s.equals(t))
|
| == (primitives) | Values directly. Safe and correct for int, double, boolean, char. |
AP Trap: Calling .equals() on null
null.equals("hello") throws a NullPointerException because null has no methods. Always place the non-null object on the left: "hello".equals(userInput) is safe even if userInput is null, because .equals() returns false for a null argument. Or null-check first: userInput != null && userInput.equals("hello").
Real-World Connection
Equivalent boolean expressions are important in software maintenance: two developers might write logically identical conditions in different forms, and a code reviewer must recognize they are the same. De Morgan's Law is used constantly to simplify complex guard conditions. Object reference comparison matters in every system that manages collections of objects: a database query returns new object instances even for the same data row, so == will return false even when the records are identical. Java's entire .equals() convention exists to solve this problem.
Summary
- Equivalent expressions produce the same boolean result for every possible input. Prove with a truth table. (EK 2.6.A.1)
-
De Morgan's Law:
!(a && b)≡!a || !band!(a || b)≡!a && !b. Distribute NOT, flip operands, flip operator. (EK 2.6.A.2) - Common De Morgan's mistake: forgetting to flip AND to OR (or vice versa).
- == on objects compares memory addresses, not content. (EK 2.6.B)
- .equals() compares object content. Use for Strings and other objects.
-
null check:
obj == nullorobj != null. Never call .equals() on a reference that may be null without checking first. -
Safe pattern:
knownNonNull.equals(possiblyNull)orobj != null && obj.equals(other).
Equivalent Boolean Expressions
Two boolean expressions are equivalent if they evaluate to the same value for all possible inputs. Truth tables are a systematic way to prove equivalence.
Example: Proving Equivalence
// Are x > 5 and !(x <= 5) equivalent? x = 3: x>5 = false, !(x<=5) = !(true) = false ✓ x = 5: x>5 = false, !(x<=5) = !(true) = false ✓ x = 7: x>5 = true, !(x<=5) = !(false) = true ✓ // Same result for all inputs → EQUIVALENT
De Morgan’s Law
De Morgan’s Law provides a rule for distributing NOT across AND/OR:
De Morgan’s Law
!(a && b) is equivalent to !a || !b !(a || b) is equivalent to !a && !b
Memory trick: when you push NOT inside, flip AND to OR (and vice versa).
De Morgan’s in Practice
// Original: not (raining AND cold) !(isRaining && isCold) // Equivalent via De Morgan's: !isRaining || !isCold // "not raining, or not cold"
AP Trap: Common De Morgan’s Mistake
Students often write !(a && b) as !a && !b. This is WRONG. The AND must become OR when you distribute the NOT. Always flip the operator.
Comparing Object References
With reference types (objects), == and != compare references (memory addresses), not content. A reference can be compared to null to check whether it points to any object.
Reference Comparison
String s = null; System.out.println(s == null); // true: no object // Two variables can reference the same object String a = "hello"; String b = a; // b and a reference the SAME object System.out.println(a == b); // true: same reference
AP Trap: == on Strings
Two String variables with identical content may NOT be == if they reference different objects. Use .equals() to compare string content. You will practice this more in Lesson 2.10.
Practice Questions
!(x > 5 || y < 3) by De Morgan’s Law?!(x > 5) || !(y < 3)
x <= 5 || y >= 3
x <= 5 && y >= 3
x > 5 && y < 3
!(a && b)?!a && !b
a || b
!a && b
!a || !b
x >= 5 and !(x < 5) equivalent?String s = null; System.out.println(s == null);
false
true
I.
!(p && q) → !p || !qII.
!(p || q) → !p && !qIII.
!(p && q) → !p && !q
a and b both hold references to the same object. What does a == b evaluate to?true, because they reference the same object in memoryfalse, because == always compares contentx < 3 || x > 7?!(x >= 3 && x <= 7)
!(3 <= x && x <= 7)
x > 7 || x < 3
!(x < 3) && !(x > 7)
!(true || false) && true?true
false
true || false
Mastery: Boolean Equivalence
a && b is equivalent to !(! a || !b). Is this correct?p || q and p && q are NOT equivalent?int x = 4; boolean p = x >= 5; boolean q = !(x < 5); System.out.println(p == q);
false
true
4
obj is passed to a method. Inside the method, the code runs if (obj != null) before calling methods on it. Why is this good practice?Truth Tables as Proof
A truth table lists every possible combination of true/false inputs and shows the output of an expression. For two variables (p, q), there are exactly 4 rows. If two expressions produce the same column of outputs, they are equivalent. Build a truth table for !(p && q) and !p || !q and verify De Morgan’s Law holds in all 4 cases.
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]