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) is x >= 5, !(x == y) is x != 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).
  • .equals asks “do these have the same contents?” Use it for Strings.
  • null means “no object”. Comparing a reference to null with == is fine; calling a method on null throws a NullPointerException.
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. .equals compares 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.

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);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

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.

Equivalence by Truth Table
Columns 1 and 2 should match. Columns 3 and 4 should match.

Activity 2: Same Meaning?

For an int x, sort each expression by what it is equivalent to.

Sort by Equivalent Meaning
Rewrite each one without the ! first, then decide.

Click an item, then click a bucket. Or drag it. Click an item inside a bucket to send it back.

Same as x >= 5
Same as x < 5
Neither

Activity 3: Objects, References, and equals

== or .equals?
Three questions about comparing objects.
Question 1

What does this print?

String a = new String("hello");
String b = new String("hello");
System.out.println(a == b);
System.out.println(a.equals(b));
Question 2

What does this print?

String a = "team";
String b = a;
System.out.println(a == b);
Question 3

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.

Simplify the Expression
Type the equivalent expression.
Question 1

Rewrite !(a > b) without the !.

Question 2

Rewrite !(x == 0 || y == 0) using De Morgan's Law.

Question 3

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).

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);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Homework Hack

Two short tasks.

  1. 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.
  2. Answer the quick check.

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);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...
Homework Quick Check
Type true or false.

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

All Java lessons