2.06 Comparing Boolean Expressions
Prove two Boolean expressions are equivalent with truth tables and De Morgan's Laws, and compare objects correctly.
- Reference Guide
- College Board Requirements
- Lesson Plan
- Code Examples
- A. Simple: Equivalence in Code
- Code Runner Challenge
- Hacks & Practice Tasks
- Activity 1: Prove De Morgan’s Laws
- Activity 2: Same Meaning?
- Activity 3: Objects, References, and equals
- Activity 4: Remove the NOT
- Popcorn Hack
- Code Runner Challenge
- Homework Hack
- Code Runner Challenge
- References
Reference Guide
Key Ideas
- Two Boolean expressions are equivalent when they have the same value for every possible input. A truth table proves it.
- De Morgan’s Laws move a
!inside parentheses by flipping the operator:!(a && b)is equivalent to!a || !b!(a || b)is equivalent to!a && !b
- A
!in front of a comparison flips the comparison:!(x < 5)isx >= 5,!(x == y)isx != y. - Simplifying
!makes conditions easier to read and harder to get wrong.
Comparing Objects
- For primitives (
int,double,boolean,char) use==. - For objects,
==asks “are these the same object?” (two references to one object are called aliases). .equalsasks “do these have the same contents?” Use it forStrings.nullmeans “no object”. Comparing a reference tonullwith==is fine; calling a method onnullthrows aNullPointerException.
| Flip rule | Before | After |
|---|---|---|
| NOT less than | !(x < 5) |
x >= 5 |
| NOT greater or equal | !(x >= 5) |
x < 5 |
| NOT equal | !(x == y) |
x != y |
| De Morgan (AND) | !(a && b) |
!a \|\| !b |
| De Morgan (OR) | !(a \|\| b) |
!a && !b |
College Board Requirements
AP CSA Unit 2, Topic 2.6: Comparing Boolean Expressions.
This lesson is built directly against the College Board’s stated learning objective for this topic:
Compare and contrast equivalent Boolean expressions, and compare object references.
Topic 2.6, Comparing Boolean Expressions, is required content on the AP Computer Science A Exam. The learning objective quoted above is the College Board’s own wording for this topic, and the activities, Popcorn Hack and Homework Hack are all written against it.
Lesson Plan
Learning Objective: Compare and contrast equivalent Boolean expressions, and compare object references.
Success Criteria: You can show two conditions are equivalent with a truth table, apply De Morgan’s Laws to remove a !, and explain why == on objects does not compare their contents.
Tech Talk & Introduction
Two conditions are equivalent when they produce the same result for every input, not just the ones you tried.
- A truth table proves equivalence by listing every combination.
- De Morgan’s Laws:
!(a && b)equals!a || !b, and!(a || b)equals!a && !b. - On objects,
==compares references, meaning whether two variables point at the same object..equalscompares contents. For Strings this distinction causes real bugs.
Code Examples
A. Simple: Equivalence in Code
Run this program. Each pair of lines should print the same value for every input. Change x, raining, and windy and confirm.
Equivalent expressions and object comparison. Change the values and confirm each pair matches.
public class Equivalent {
public static void main(String[] args) {
int x = 7;
boolean raining = true;
boolean windy = false;
System.out.println("!(x < 5): " + !(x < 5));
System.out.println("x >= 5: " + (x >= 5));
System.out.println("!(raining && windy): " + !(raining && windy));
System.out.println("!raining || !windy: " + (!raining || !windy));
System.out.println("!(raining || windy): " + !(raining || windy));
System.out.println("!raining && !windy: " + (!raining && !windy));
String first = new String("java");
String second = new String("java");
String alias = first;
System.out.println("first == second: " + (first == second));
System.out.println("first.equals(second): " + first.equals(second));
System.out.println("first == alias: " + (first == alias));
}
}
Code Runner Challenge
Equivalent expressions and object comparison. Change the values and confirm each pair matches.
View IPYNB Source
// CODE_RUNNER: Equivalent expressions and object comparison. Change the values and confirm each pair matches.
public class Equivalent {
public static void main(String[] args) {
int x = 7;
boolean raining = true;
boolean windy = false;
System.out.println("!(x < 5): " + !(x < 5));
System.out.println("x >= 5: " + (x >= 5));
System.out.println("!(raining && windy): " + !(raining && windy));
System.out.println("!raining || !windy: " + (!raining || !windy));
System.out.println("!(raining || windy): " + !(raining || windy));
System.out.println("!raining && !windy: " + (!raining && !windy));
String first = new String("java");
String second = new String("java");
String alias = first;
System.out.println("first == second: " + (first == second));
System.out.println("first.equals(second): " + first.equals(second));
System.out.println("first == alias: " + (first == alias));
}
}
Equivalent.main(null);
Hacks & Practice Tasks
Activity 1: Prove De Morgan’s Laws
Fill in the table. If two columns match on every row, the expressions are equivalent.
Activity 2: Same Meaning?
For an int x, sort each expression by what it is equivalent to.
Click an item, then click a bucket. Or drag it. Click an item inside a bucket to send it back.
Activity 3: Objects, References, and equals
What does this print?
What does this print?
Which comparison is correct for an int score and a String name?
Activity 4: Remove the NOT
Rewrite each expression without a !. Spaces do not matter.
Rewrite !(a > b) without the !.
Rewrite !(x == 0 || y == 0) using De Morgan's Law.
Rewrite !(age >= 18 && hasId) using De Morgan's Law.
Popcorn Hack
The condition below works but is hard to read. Rewrite it without any ! so it still prints the same result for every age and member combination. Test with (15, true), (20, false), and (20, true).
Popcorn hack: remove every ! using De Morgan's Law and the flip rules.
public class Simplify {
public static void main(String[] args) {
int age = 15;
boolean member = true;
// Rewrite this condition with no ! at all
if (!(age >= 18 || !member)) {
System.out.println("Youth member rate");
} else {
System.out.println("Standard rate");
}
}
}
Code Runner Challenge
Popcorn hack: remove every ! using De Morgan's Law and the flip rules.
View IPYNB Source
// CODE_RUNNER: Popcorn hack: remove every ! using De Morgan's Law and the flip rules.
public class Simplify {
public static void main(String[] args) {
int age = 15;
boolean member = true;
// Rewrite this condition with no ! at all
if (!(age >= 18 || !member)) {
System.out.println("Youth member rate");
} else {
System.out.println("Standard rate");
}
}
}
Simplify.main(null);
Homework Hack
Two short tasks.
- Finish the runner: the password checker has a bug because it uses
==on Strings. Fix it, then add the second check described in the comment. - Answer the quick check.
Homework: fix the String comparison and add a length check with no !.
public class PasswordCheck {
public static void main(String[] args) {
String saved = new String("open-sesame");
String typed = new String("open-sesame");
// TODO 1: this comparison is wrong for Strings. Fix it.
if (typed == saved) {
System.out.println("Welcome back!");
} else {
System.out.println("Wrong password.");
}
// TODO 2: print "Too short" if typed has fewer than 8 characters,
// otherwise print "Length OK". Do it without using ! anywhere.
}
}
Code Runner Challenge
Homework: fix the String comparison and add a length check with no !.
View IPYNB Source
// CODE_RUNNER: Homework: fix the String comparison and add a length check with no !.
public class PasswordCheck {
public static void main(String[] args) {
String saved = new String("open-sesame");
String typed = new String("open-sesame");
// TODO 1: this comparison is wrong for Strings. Fix it.
if (typed == saved) {
System.out.println("Welcome back!");
} else {
System.out.println("Wrong password.");
}
// TODO 2: print "Too short" if typed has fewer than 8 characters,
// otherwise print "Length OK". Do it without using ! anywhere.
}
}
PasswordCheck.main(null);
Are !(a && !b) and !a || b equivalent? Type true or false.
References
College Board Course and Exam Description
Topic 2.6, Comparing Boolean Expressions, is required content on the AP Computer Science A Exam. The learning objective quoted in section 3 (College Board Requirements) is the College Board’s own wording for this topic, and the activities, Popcorn Hack and Homework Hack are all written against it.
Outside Academic Reference
Reference equality is the defined behaviour of == on operands of reference type: it tests whether both operands refer to the same object, which is why two Strings with identical characters can compare as unequal.
Reference List
College Board. (2025). AP Computer Science A: Course and exam description. https://apcentral.collegeboard.org/courses/ap-computer-science-a
Gosling, J., Joy, B., Steele, G., Bracha, G., Buckley, A., Smith, D., & Bierman, G. (2023). The Java language specification: Java SE 21 edition. Oracle America. https://docs.oracle.com/javase/specs/jls/se21/html/jls-15.html#jls-15.21