6m left·0%
Reading Time: 6 min
Last Updated: February 26, 2026
Main Ideas: 4
Reading Time: 6 min
Last Updated: February 26, 2026
Main Ideas: 4

Topic 2.6 Notes – Comparing Boolean Expressions

Verified for 2027 AP® Computer Science A Exam
Read aloud
Boolean expressions decide which code runs and which code doesn’t. In this topic, you’re learning when two different-looking Boolean expressions actually mean the same thing, and how Java compares object references using ==, !=, null, and .equals(). This shows up constantly in conditionals and exam questions that test logical reasoning.

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, true
  • true, false
  • false, true
  • false, false

Here’s an example comparing !(a && b) and !a || !b:

aba && b!(a && b)!a || !b
truetruetruefalsefalse
truefalsefalsetruetrue
falsetruefalsetruetrue
falsefalsefalsetruetrue

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.

  1. !(a && b) ⇔ !a || !b
  2. !(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)
  1. Push the ! inside
  2. Flip && to ||
  3. 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 > 10 instead 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 == null
  • obj != 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.

Key Takeaways

Two Boolean expressions are equivalent only if they match for every possible input.
De Morgan’s Laws require flipping both the operator (&&/||) and every condition inside.
== compares object references, not object content.
Use .equals() to compare String content on the AP exam.
Always check obj != null before calling a method to avoid a NullPointerException.
If even one truth-table row differs, the expressions are not equivalent.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse this website.

Notes

1 credit used · 5/5 remaining