Skip to main content

Unit 2 · Topic 2.6

2.6 Comparing Boolean Expressions

Two Boolean expressions can look different and still always give the same answer. This topic shows how to prove that with a truth table, how De Morgan's laws rewrite a negated condition, and how comparing objects with == differs from comparing them with equals.

Key terms

  • equivalent expressions
  • De Morgan's laws
  • truth table
  • alias
  • equals method

Equivalent expressions

Two Boolean expressions are equivalent if they give the same result for every possible combination of values. To prove it, build a truth table with a row for each combination and check the two result columns match in every row. To prove two expressions are not equivalent, you only need one row where they differ.

De Morgan's laws

De Morgan's laws tell you how to push a ! into parentheses. When the ! moves inside, each part gets negated, and && and || swap:

  • !(a && b) is equivalent to !a || !b
  • !(a || b) is equivalent to !a && !b

Negating comparisons

When a and b are comparisons, negate each one by flipping it, not just the direction of the symbol:

ComparisonIts negation
x > 3x <= 3
x < 3x >= 3
x == 3x != 3

Checking De Morgan with a truth table

Here's !(a && b) against !a || !b. The column labeled "left side" is !(a && b) and the one labeled "right side" is !a || !b. They match in every row, so the expressions are equivalent.

aba && bleft sideright side
truetruetruefalsefalse
truefalsefalsetruetrue
falsetruefalsetruetrue
falsefalsefalsetruetrue

Comparing object references

With objects, == and != compare references. Two variables that refer to the same object are called aliases, and == is true for them. Two different objects give false, even when they hold identical data.

You can compare a reference with null using == or != to check whether it refers to an object at all. Calling a method on a null reference throws a NullPointerException, so a check like name != null && name.equals("Ava") uses short-circuiting to stay safe.

To compare contents, classes provide an equals method that decides what "the same" means, usually by comparing attributes. For String, equals checks that the characters match.

String first = new String("map"); String second = new String("map"); String third = first; String nothing = null; System.out.println(first == second); System.out.println(first.equals(second)); System.out.println(first == third); System.out.println(nothing == null);

This prints false, true, true, true: different objects, same letters, aliases, and a null check.

Not on the exam

You won't be asked to write your own equals method for a class. You just need to use the ones classes already have.

Worked examples

Try each one yourself first, then open the solution.

  1. Example 1

    Applying De Morgan's laws

    Which expression is equivalent to !(x > 3 && y <= 5), where x and y are int variables? (A) x > 3 || y <= 5 (B) x <= 3 && y > 5 (C) x <= 3 || y > 5 (D) x < 3 || y >= 5

    Show the solution
    1. Step 1: De Morgan: !(a && b) becomes !a || !b. So the && turns into ||, which rules out (B).
    2. Step 2: Negate each comparison. The opposite of x > 3 is x <= 3 (3 itself now counts). The opposite of y <= 5 is y > 5.
    3. Step 3: That gives x <= 3 || y > 5. Choice (D) flips the symbols but gets the boundaries wrong: with x = 3 and y = 2, the original is true and (D) is false.
    4. Step 4: As a check, try x = 2 and y = 9: the original is !(false && false), which is true, and (C) is true || true, also true. Because every step followed De Morgan's law, (C) agrees with the original for every possible x and y, not just these.

    Answer: (C) x <= 3 || y > 5.

  2. Example 2

    == versus equals

    What does this code print?String first = new String("map"); String second = new String("map"); String third = first; String nothing = null; System.out.println(first == second); System.out.println(first.equals(second)); System.out.println(first == third); System.out.println(nothing == null);

    Show the solution
    1. Step 1: first and second are made with separate new calls, so they're two different objects. first == second is false.
    2. Step 2: They hold the same characters, so first.equals(second) is true.
    3. Step 3: third = first copies the reference, making third an alias of first. first == third is true.
    4. Step 4: nothing holds null, so nothing == null is true.

    Answer: Four lines: false, true, true, true.

Common mistakes

  • Moving the ! inside without swapping && and ||. Both changes are needed.
  • Negating x > 3 as x < 3. The opposite of "greater than" is "less than or equal to".
  • Using == to compare the contents of two objects. It only tells you whether they're the same object.
  • Calling equals on a variable that might be null. Check for null first.

On the exam

  • A classic multiple-choice question gives a negated compound condition and asks which expression is equivalent. Apply De Morgan step by step, then test a boundary value to be sure.
  • To show two expressions are not equivalent, find one set of values where they disagree. That's often faster than a full truth table.

Connected topics

Videos

  • AP Computer Science A - Topic 2.6 - Part 1: Comparing Boolean Expressions

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • AP CS A 3.6 - Equivalent Boolean Expressions

    CodeHSWatch on YouTube (opens in a new tab)

  • De Morgan's Laws For Computer Science and Programming

    Bill BarnumWatch on YouTube (opens in a new tab)

  • AP Computer Science A - Topic 2.6 - Part 2: Comparing Boolean Expressions

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • AP CSA - Unit 3: If Statements - Lesson 6: Comparing Objects

    Goldie's Math EmporiumWatch on YouTube (opens in a new tab)

  • .equals() vs. == in Java - The Real Difference

    Coding with JohnWatch on YouTube (opens in a new tab)

Check yourself

4 questions on 2.6 Comparing Boolean Expressions. Pick an answer to see if you got it, and why.

Question 1 of 4

Assume x and y are int variables. Which of the following expressions is equivalent to !(x > 3 && y != 0)?

Question 2 of 4

Assume a and b are boolean variables. Which of the following expressions is equivalent to !(a || !b)?

abResult
truetruefalse
truefalsetrue
falsetruetrue
falsefalsefalse

Truth table

Question 3 of 4

Which of the following expressions produces the Result column for all four rows of the table?

Question 4 of 4

Assume n is an int variable. Which of the following best describes the values of n for which !(n < 5) && !(n > 10) evaluates to true?

0 of 4 answered