Comparing
Boolean Expressions
Learn how to determine when two Boolean expressions are logically equivalent, use truth tables to prove it, and apply De Morgan's Laws to simplify complex conditions.
What Is a Boolean Expression?
A Boolean expression is any expression in Java that evaluates to exactly one of two values: true or false. They are the building blocks of every decision your program makes.
What Does Equivalent Mean?
Two Boolean expressions are logically equivalent if they produce the same result for every possible combination of input values. They may look different in code, but they behave identically.
Why Compare Them?
Truth Tables: The Core Tool
A truth table lists every possible combination of inputs and the resulting output. If two expressions have identical output columns, they are equivalent.
| \(p\) | \(q\) | \(p \&\& q\) | \(!(!p \|\| !q)\) |
|---|---|---|---|
| true | true | true | true |
| true | false | false | false |
| false | true | false | false |
| false | false | false | false |
De Morgan's Laws
De Morgan's Laws are the most important equivalences on the AP exam. They tell you how NOT distributes over AND and OR:
De Morgan's Visualized
Negating Relational Operators
When NOT is applied to a simple comparison, you can replace it with the opposite operator. No need for De Morgan's here:
| Original | Negation |
|---|---|
| \(!(a == b)\) | \(a \ne b\) |
| \(!(a \ne b)\) | \(a == b\) |
| \(!(a < b)\) | \(a \ge b\) |
| \(!(a > b)\) | \(a \le b\) |
| \(!(a \le b)\) | \(a > b\) |
| \(!(a \ge b)\) | \(a < b\) |
== vs .equals()
When comparing objects (especially Strings), == and .equals() are not equivalent. This is the most common source of Boolean bugs in Java.
How to Compare Step by Step
Example 1: Applying De Morgan's
Simplify: \(!(x > 3 \&\& y < 10)\)
Example 2: Truth Table Proof
Are these equivalent? \(!(a \|\| b)\) and \(!a \&\& !b\)
| \(a\) | \(b\) | \(!(a \|\| b)\) | \(!a \&\& !b\) |
|---|---|---|---|
| T | T | F | F |
| T | F | F | F |
| F | T | F | F |
| F | F | T | T |
Example 3: Non-Equivalent Trap
Are these equivalent? \(!(a \&\& b)\) and \(!a \&\& !b\)
| \(a\) | \(b\) | \(!(a \&\& b)\) | \(!a \&\& !b\) |
|---|---|---|---|
| T | T | F | F |
| T | F | T | F |
| F | T | T | F |
| F | F | T | T |
Short-Circuit Evaluation
Java uses short-circuit evaluation for \(&&\) and \(||\). This affects behavior but not logical equivalence (when expressions have no side effects).
Common Mistakes
Equivalence Summary
| Expression | Equivalent Form | Rule |
|---|---|---|
| \(!(p \&\& q)\) | \(!p \|\| !q\) | De Morgan's 1 |
| \(!(p \|\| q)\) | \(!p \&\& !q\) | De Morgan's 2 |
| \(!(a < b)\) | \(a \ge b\) | Negate relational |
| \(!(a == b)\) | \(a \ne b\) | Negate equality |
| \(!!p\) | \(p\) | Double negation |
| \(p \&\& \text{true}\) | \(p\) | Identity |
| \(p \|\| \text{false}\) | \(p\) | Identity |
Question 1 of 5
Which expression is equivalent to \(!(x > 10 \&\& y < 5)\)?
Question 2 of 5
What is the result of \(!(\text{false} \|\| \text{true})\)?
Question 3 of 5
Which pair of expressions is NOT equivalent?
Question 4 of 5
Given int x = 7, what does \(!(x \ge 7 \|\| x < 3)\) evaluate to?
Question 5 of 5
Which expression is equivalent to \(!(a \ne b)\)?