Fiveable

💻AP Computer Science A Unit 2 Review

QR code for AP Computer Science A practice questions

2.6 Equivalent Boolean Expressions

2.6 Equivalent Boolean Expressions

Written by the Fiveable Content Team • Last updated June 2026
Verified for the 2027 exam
Verified for the 2027 examWritten by the Fiveable Content Team • Last updated June 2026
💻AP Computer Science A
Unit & Topic Study Guides

Frequently Asked Questions

Previous Exam Prep

Study Tools

Exam Skills

AP Cram Sessions 2021

Pep mascot

Two Boolean expressions are equivalent when they evaluate to the same value in every possible case, and you can prove that using a truth table. The main tool is De Morgan's law: !(a && b) is equivalent to !a || !b, and !(a || b) is equivalent to !a && !b. For AP Computer Science A, use these rules to trace conditions and rewrite logic without changing behavior.

Why This Matters for the AP Computer Science A Exam

Comparing Boolean expressions shows up when you trace code and when you write conditions. On multiple-choice questions, you may need to decide whether two conditions behave the same way or predict the output of a tricky negated condition. When you write free-response code, rewriting a condition with De Morgan's law can make your logic cleaner and help you avoid mistakes with negation.

Object comparison is just as important. Knowing the difference between == (same reference) and .equals() (same contents) helps you avoid logic errors when your code compares Strings or other objects, which is a common source of wrong answers.

Key Takeaways

  • Two Boolean expressions are equivalent if they evaluate to the same result for every possible combination of inputs, and a truth table can prove it.
  • De Morgan's law: !(a && b) is equivalent to !a || !b, and !(a || b) is equivalent to !a && !b.
  • When you negate a compound condition, flip each part and switch && with ||.
  • == and != compare object references, so they check whether two variables point to the same object, not whether the contents match.
  • Compare a reference to null with == or != to check whether it actually refers to an object.
  • Use a class's .equals() method to compare two objects by their contents.

Logical Equivalence

Two Boolean expressions are equivalent if they produce the same value in every possible case. It is not enough for them to match sometimes. They have to match for every combination of true and false inputs.

A truth table is the reliable way to prove equivalence. List every combination of the variables, evaluate both expressions for each row, and check that the result columns are identical.

Here is a truth table proving !(a && b) equals !a || !b:

| a | b | a && b | !(a && b) | !a | !b | !a \ | \ | !b | |:--|:--|:--|:--|:--|:--|:--| | true | true | true | false | false | false | false | | true | false | false | true | false | true | true | | false | true | false | true | true | false | true | | false | false | false | true | true | true | true |

The !(a && b) column and the !a || !b column match in every row, so the two expressions are equivalent.

De Morgan's Law

De Morgan's law tells you how to push a ! (not) across an && or ||. These are the two forms to memorize:

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

The pattern: when a ! moves inside the parentheses, negate each part and switch the operator. && becomes ||, and || becomes &&.

This is useful for rewriting a confusing negated condition into something clearer.

</>Java
// These two conditions are equivalent
boolean reject1 = !(username != null && password != null);
boolean reject2 = username == null || password == null;

Notice how != flips to == and && flips to || when the negation is distributed.

Comparing Object References

When you compare objects, you have to be careful about what you are actually checking.

== and != compare references. That means they check whether two variables point to the exact same object in memory, not whether two separate objects hold the same data.

</>Java
String s1 = new String("hi");
String s2 = new String("hi");

System.out.println(s1 == s2);        // false: different objects
System.out.println(s1.equals(s2));   // true: same contents

Two different variables can also reference the same object. When that happens, == returns true because both point to the same place.

</>Java
String a = "cat";
String b = a;        // b now references the same object as a
System.out.println(a == b);  // true

Comparing to null

You can compare a reference to null using == or != to check whether it actually refers to an object.

</>Java
String name = null;
if (name == null) {
    System.out.println("No object assigned yet");
}

This check matters because calling a method on a null reference causes an error.

Using equals for contents

Many classes define their own .equals() method, which compares objects based on their attributes instead of their references. When you want to know whether two objects hold the same data, use .equals(), not ==.

</>Java
String first = "apple";
String second = "apple";
if (first.equals(second)) {
    System.out.println("Same contents");
}

A common safe pattern combines a null check with .equals() so you do not call a method on a null reference:

</>Java
if (name != null && name.equals("admin")) {
    // safe: the equals call only runs when name is not null
}

Because && uses short-circuit evaluation, the second part runs only when the first part is true, so the .equals() call is skipped when name is null.

How to Use This on the AP Computer Science A Exam

Code Tracing

When a question hands you two conditions and asks if they always behave the same, build a quick truth table. Plug in each combination of true and false, evaluate both sides, and compare. If even one row differs, the expressions are not equivalent.

For a negated condition, apply De Morgan's law before you trace. Rewriting !(a && b) as !a || !b often makes the logic easier to follow and reduces mistakes.

Free Response

When you write conditions in free-response code, a clean condition is easier to get right. If you find yourself writing a tangled negation, distribute the ! with De Morgan's law and switch the operator.

For object comparisons in your code, use .equals() when you want to compare contents and == when you want to compare references or check for null. Mixing these up is an easy way to produce code that compiles but gives wrong answers.

Common Trap

Watch for == used on Strings or other objects when the question expects a content comparison. "apple" == new String("apple") is false because the references differ, but .equals() returns true.

Common Misconceptions

  • Equivalent does not mean "looks similar." Two expressions are equivalent only if they match for every possible input, which a truth table confirms.
  • De Morgan's law is not just dropping the !. You must negate each part and switch && with ||. !(a && b) is not !a && !b.
  • == does not compare contents for objects. It compares references, so two separate objects with the same data return false with ==.
  • .equals() and == are not interchangeable for objects. Use .equals() for contents and == for references or null checks.
  • Comparing a String to null is fine with == and !=, but calling a method on a null reference causes an error, which is why the null check comes first in the name != null && name.equals(...) pattern.
  • Overriding the equals method is outside the scope of this course, so you only need to use a class's existing .equals(), not write your own.

Vocabulary

The following words are mentioned explicitly in the College Board Course and Exam Description for this topic.

Term

Definition

!=

The not-equal operator used to determine if two object references are different or if a reference does not equal a value.

==

The equality operator used to compare object references or values; for objects, it checks if two variables reference the same object in memory.

Boolean expression

An expression that evaluates to either true or false, used to control the execution of loops and conditional statements.

De Morgan's law

A logical rule that allows conversion between equivalent Boolean expressions: !(a && b) is equivalent to !a || !b, and !(a || b) is equivalent to !a && !b.

equals method

A String method that returns true if the string contains the same sequence of characters as another string, and false otherwise.

equivalency

The state of two objects being equal based on specified criteria, typically determined by comparing their attributes.

equivalent

Having the same value or producing the same result; two Boolean expressions are equivalent if they evaluate to the same value in all cases.

null

A special value indicating that a reference variable does not currently reference any object.

object reference

A value that points to the memory location where an object is stored, allowing access to that object.

truth table

A table that shows all possible input combinations and their corresponding output values, used to prove whether Boolean expressions are equivalent.

Frequently Asked Questions

What are equivalent Boolean expressions in AP CSA?

Equivalent Boolean expressions evaluate to the same true or false result for every possible combination of input values.

How can a truth table prove Boolean expressions are equivalent?

A truth table lists every possible combination of Boolean values. If the final result columns match in every row, the expressions are equivalent.

What is De Morgan's law in Java?

De Morgan's law says !(a && b) is equivalent to !a || !b, and !(a || b) is equivalent to !a && !b.

How do you negate a compound Boolean expression?

Distribute the ! to each part, reverse each comparison when needed, and switch && with || or || with &&.

What is the difference between == and .equals() in Java?

For objects, == checks whether two references point to the same object, while .equals() checks content based on the class's equals method.

How should you compare an object reference to null?

Use == or != to compare a reference with null. Check for null before calling a method like .equals() to avoid a null reference error.

Pep mascot
Upgrade your Fiveable account to print any study guide

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Click below to go to billing portal → update your plan → choose Yearly→ and select "Fiveable Share Plan". Only pay the difference

Plan is open to all students, teachers, parents, etc
Pep mascot
Upgrade your Fiveable account to export vocabulary

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Plan is open to all students, teachers, parents, etc
report an error
description

screenshots help us find and fix the issue faster (optional)

add screenshot