2.6 Comparing Boolean Expressions
💻AP Computer Science A Unit 2 Review
2.6 Comparing Boolean Expressions
💻 AP Computer Science A
2.1
Algorithms with Selection and Repetition
2.2
Boolean Expressions
2.3
If Statements and Control Flow
2.4
If-Else Statements
2.5
Compound Boolean Expressions
2.6
Equivalent Boolean Expressions
2.7
While Loops
2.8
For Loops
2.9
Implementing Selection and Iteration Algorithms
2.10
Developing Algorithms Using Strings
2.11
Nested Iteration
2.12
Informal Code Analysis
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.
Equivalent Conditions and Object Comparison Rules
- 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
nullwith==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.
</>JavaString 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.
</>JavaString 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.
</>JavaString 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 ==.
</>JavaString 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:
</>Javaif (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.
Checking Equivalent Conditions and Comparing Objects
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
- Misconception: Equivalent means expressions look similar.Actually: Equivalent does not mean "looks similar." Two expressions are equivalent only if they match for every possible input, which a truth table confirms.
- Misconception: De Morgan's law is just dropping the
!.Actually: De Morgan's law is not just dropping the!. You must negate each part and switch&&with||.!(a && b)is not!a && !b. - Misconception:
==compares contents for objects.Actually:==does not compare contents for objects. It compares references, so two separate objects with the same data returnfalsewith==. - Misconception:
.equals()and==are interchangeable for objects.Actually:.equals()and==are not interchangeable for objects. Use.equals()for contents and==for references ornullchecks. - Misconception: A String cannot be compared to
nullwith==or!=.Actually: Comparing a String tonullis fine with==and!=, but calling a method on anullreference causes an error, which is why the null check comes first in thename != null && name.equals(...)pattern. - Misconception: You need to write your own
equalsmethod.Actually: Overriding theequalsmethod is outside the scope of this course, so you only need to use a class's existing.equals(), not write your own.
Related AP Computer Science A Guides
Practice Questions for 2.6
1 of 10
The following method is intended to return true if both p1 and p2 are not null and they have equivalent attributes. However, the method sometimes throws a NullPointerException.
</>Javapublic boolean isMatchingProfile(Profile p1, Profile p2) { // line 2 return p1.equals(p2) && p1 != null && p2 != null; }
Which of the following changes should be made to line 2 so the method works as intended and avoids the exception?
A.
return p1 != null && p2.equals(p1) && p2 != null;
B.
return p1 != null && p2 != null && p1.equals(p2);
C.
return p1 != null && p2 != null && p1 == p2;
D.
return p1 != null && p2 != null || p1.equals(p2);
Vocabulary
The following words are mentioned explicitly in the AP® course framework 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. |
See Fiveable more often in Google
Mark Fiveable as a preferred source and Google is more likely to show you our guides in Top Stories, with a "preferred" badge in AI Mode and AI Overviews.