Topic 2.6 Notes – Comparing Boolean Expressions
1. What It Means for Boolean Expressions to Be Equivalent
Two Boolean expressions are equivalent if they produce the same true/false result for every possible input.
It’s not enough that they match most of the time. One mismatch means they are not equivalent.
If expressions are equivalent, you can swap one for the other anywhere in your code without changing behavior.
Using Truth Tables to Prove Equivalence
A truth table lists every possible combination of values and shows what each expression returns.
For two variables a and b, there are 4 cases:
true, truetrue, falsefalse, truefalse, false
Here’s an example comparing !(a && b) and !a || !b:
| 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 |
The last two columns match in every row. That proves equivalence.
On the AP exam, you usually won’t build full tables. Instead, you’ll:
- Apply known laws (especially De Morgan’s)
- Mentally test edge cases
If even one case behaves differently, the expressions are not equivalent.
2. De Morgan’s Laws
These are the two equivalences you must know perfectly.
!(a && b)⇔!a || !b!(a || b)⇔!a && !b
When you negate a group:
- The
!moves inside &&and||switch- Every condition gets negated
Applying De Morgan Step by Step
Take:
!(x > 10 && y <= 3)
- Push the
!inside - Flip
&&to|| - Negate each comparison
Result:
x <= 10 || y > 3
Each comparison must be logically flipped:
>becomes<=<becomes>===becomes!=
Common Mistakes
- Negating only one condition
- Forgetting to switch
&&and|| - Writing
!x > 10instead of!(x > 10) - Forgetting that
!(a || b)becomes!a && !b, not!a || !b
If you see a large negation wrapped around parentheses on a quiz, De Morgan’s is probably involved.
3. Comparing Object References with Boolean Expressions
Up to now, you’ve mostly compared primitives. Objects behave differently.
A variable that stores an object actually stores a reference to the object.
a. Using == and != with Objects
With objects:
==checks whether two variables refer to the same object in memory!=checks whether they refer to different objects
Example:
String s1 = new String("cat");
String s2 = new String("cat");
boolean result = (s1 == s2); // false
They contain the same text, but they are different objects.
Two variables can also refer to the same object:
String s3 = s1;
boolean result2 = (s1 == s3); // true
On MCQs, they love showing diagrams where multiple variables point to the same object.
b. Comparing to null
null means the variable does not reference any object.
You can safely check:
obj == nullobj != null
This pattern is extremely common:
if (obj != null && obj.isReady())
Because Java uses short-circuit evaluation, obj.isReady() only runs if obj != null is true. That prevents a NullPointerException.
If the order were reversed, it would crash.
c. Using .equals() for Logical Equality
Many classes define an equals() method to compare content, not memory location.
Most important example is String.
String a = "dog";
String b = "dog";
boolean same = a.equals(b); // true
==→ same object.equals()→ same meaningful data
For AP CSA:
- Use
.equals()when comparing Strings - You are not responsible for writing your own
equals()method
4. When to Rewrite Boolean Expressions
Rewriting helps you:
- Remove awkward negations
- Make conditions clearer
- Match answer choices on multiple choice
- Avoid null errors
Examples:
!(score < 60)
Equivalent:
score >= 60
Double negation:
!!condition
Equivalent:
condition
On tests, you may be asked whether two expressions are equivalent after rewriting. Slow down and check all cases. One mismatch means it’s wrong.