TOPIC 2.6 · STUDY
Comparing Boolean Expressions
Two boolean expressions are equivalent when they match for every combination of values. De Morgan rewrites !(a && b) as !a || !b and !(a || b) as !a && !b. For String and object values, == compares references while equals compares characters case-sensitively; short-circuit && protects null before equals.
2.6.A
Equivalent boolean expressions
Two boolean expressions are equivalent when they produce the same result for every combination of their operands. Matching on one test case is not enough. A counterexample on any row of a truth table shows the expressions are not equivalent.
De Morgan's laws give two reliable rewrites. Negating a conjunction flips the connective to disjunction and negates each operand: !(a && b) is equivalent to !a || !b. Negating a disjunction flips the other way: !(a || b) is equivalent to !a && !b. The reverse directions hold as well: !a || !b rewrites as !(a && b), and !a && !b rewrites as !(a || b).
Negating && produces || of the negated operands. Negating || produces && of the negated operands.
boolean sunny = true;
boolean calm = false;
System.out.print(!(sunny && calm));
System.out.print("," + (!sunny || !calm));
System.out.print("," + (!(sunny || calm)));
System.out.print("," + (!sunny && !calm));
That sequence prints true,true,false,false. The first pair matches De Morgan for &&. The second pair matches De Morgan for ||. Both columns agree for these values; a full truth table would confirm they agree for every pair.
int temp = 28;
boolean icy = true;
System.out.print(!(temp >= 32 && icy));
System.out.print("," + (temp < 32 || !icy));
Relational operands follow the same rewrite. The negation of temp >= 32 is temp < 32, so !(temp >= 32 && icy) becomes temp < 32 || !icy. Both sides print true for these values.
Key points
- Equivalent expressions match for every combination of values.
- !(a && b) is equivalent to !a || !b.
- !(a || b) is equivalent to !a && !b.
- Negating a comparison also flips the relational operator, such as !(x >= 32) to x < 32.
2.6.B
Comparing objects with == and equals
The == operator on String values compares references, not characters. Two separately constructed String objects that hold the same characters are still different objects, so == is false. Assignment copies the reference: if one variable is assigned from another, both names can be == because they refer to the same object.
String left = new String("spark");
String right = new String("spark");
String alias = left;
System.out.print(left == right);
System.out.print("," + (left == alias));
System.out.print("," + left.equals(right));
System.out.print("," + "spark".equals("Spark"));
That sequence prints false,true,true,false. left and right are distinct objects, so left == right is false. alias refers to the same object as left, so left == alias is true. equals compares characters and returns true for matching content. equals is case-sensitive, so "spark".equals("Spark") is false.
Calling equals on a null reference throws a NullPointerException. Short-circuit evaluation of && avoids that call when the left operand is false. The pattern code != null && code.equals("OK") checks for null first; when code is null, equals is never called and the compound expression is false.
String code = null;
System.out.print(code != null && code.equals("OK"));
code != null is false, so && skips code.equals("OK"). The print is false and no NullPointerException is thrown.
Key points
- == compares object references, not String characters.
- Separately constructed strings with the same characters are not ==.
- Assignment copies a reference, so two names can be == for the same object.
- equals compares characters and is case-sensitive.
- code != null && code.equals("OK") skips equals when code is null.
== does not compare String characters; use equals when you need matching content.