From the free course AP Computer Science A on Lykke
2.6 Comparing Boolean Expressions
Key concepts: Equivalent Boolean expressions · Comparing Boolean expressions · De Morgan’s laws · Logical negation with ! · Logical OR (||) · Logical AND (&&) · Comparing object references · The equals method · Boolean expressions in conditional statements · Determining results from statement execution order
Two Boolean expressions are equivalent when they produce the same Boolean result in every possible case, even if their symbols and structure look different. The practical payoff is powerful: you can replace a complicated condition with a clearer one without changing the program’s behavior.
2.6 Comparing Boolean Expressions
Two Boolean expressions are equivalent when they produce the same Boolean result in every possible case, even if their symbols and structure look different. The practical payoff is powerful: you can replace a complicated condition with a clearer one without changing the program’s behavior.
Equivalent expressions and truth tables
A truth table lists every possible combination of Boolean inputs and records the result of each expression. For two expressions to be equivalent, their result columns must match on every row—not merely on the examples you happen to test.
For example, compare !(a || b) with !a && !b. The first expression says “it is not true that at least one condition holds.” The second says “neither condition holds.” Those descriptions differ in wording but agree in every case.
| a | b | !(a || b) | !a && !b |
|---|---|---:|---:|
| false | false | true | true |
| false | true | false | false |
| true | false | false | false |
| true | true | false | false |
Because the final two columns match in all cases, the expressions are equivalent. This is the second form of De Morgan’s laws:
!(a || b)is equivalent to!a && !b.
The other De Morgan law changes a negated AND into an OR:
A reliable transformation has two steps: move the negation inward, then change the connecting operator. A negated || becomes &&; a negated && becomes ||; each individual Boolean condition is negated.
Worked example: rewriting a condition
Suppose a library allows entry only when a visitor is not a minor and does not need supervision:
!(minor || needsSupervision)
Applying De Morgan’s law gives:
The rewritten condition is often easier to read because it states the two requirements directly. It is not a new algorithm; it is an equivalent implementation of the same algorithm, satisfying Learning Objective 2.6.A: Compare equivalent Boolean expressions and Essential Knowledge 2.6.A.1, which defines equivalence through equal results in all cases. Truth tables provide the proof technique required by Essential Knowledge 2.6.A.2.
Misconception check — “matching on my test cases proves equivalence.” Testing a few inputs can suggest equivalence, but it cannot prove it for every possible input. For two variables, a complete truth table has four rows; for more variables, systematic reasoning or algebraic transformations are needed.
Comparing object references
Boolean expressions can also compare variables that hold object references. A reference is a value that identifies an object; it is not the object’s internal data. Two variables may hold references to the same object, even though the variables have different names.
String first = new String("maple");
String second = first;
String third = new String("maple");
System.out.println(first == second); // true
System.out.println(first == third); // false
System.out.println(first.equals(third)); // true
Here, first and second refer to one object, so first == second is true. third contains the same character sequence but refers to a separately created object, so first == third is false. The class-defined equals method compares objects according to their meaningful contents, making first.equals(third) true.
This distinction expresses Essential Knowledge 2.6.B.1 and 2.6.B.3:
==and!=compare object references—whether two variables identify the same object..equals()can compare object equivalence according to attributes defined by the class.- A class may define its own meaning of equality.
An object reference may also be compared with null, a special value meaning that the reference identifies no object:
if (ticket == null) {
System.out.println("No ticket selected.");
}
This applies Essential Knowledge 2.6.B.2. Calling an instance method through a null reference causes an error, so checking reference != null can be necessary before using the object.
Misconception check — “== always compares object contents.” For primitive values, == compares values. For object references, it compares identity. Use the class’s .equals() method when the question is whether two objects represent equivalent data.
The topic develops Learning Objective 2.6.B: Develop code to compare object references using Boolean expressions and determine the result of these expressions. It also exercises 2.A: Write program code to implement an algorithm when a comparison controls a condition, and 3.A: Determine the result or output based on statement execution order in an algorithm when tracing the resulting Boolean values and output.
Retrieval check:
- Which expression is equivalent to
!(p && q)? - If
String x = new String("red"); String y = new String("red");, isx == ytrue or false? Isx.equals(y)true or false? - What does
nullindicate when stored in an object-reference variable?
Answers:
!p || !q.x == yisfalsebecause the objects are distinct;x.equals(y)is ordinarilytruebecause their character data matches.- The variable does not currently refer to an object. Overriding
equalsto create a new equality definition is outside the required scope; using an existing.equals()method is the essential skill.


Source Materials
- AP Computer Science A Course and Exam Description
- AP Computer Science A Course and Exam Description Clarifications and Corrections
- AP Computer Science A Course Overview
- AP Computer Science A Course at a Glance
- AP Computer Science A Course at a Glance Poster
- Adopt AP Computer Science A
- AP Computer Science A
- AP Computer Science A
- boolean equals(Object other)