AP-Computer-Science ScienceAP
Logic Logic
Logical operators
| Name | Meaning | Java symbol | Short-circuit behavior |
|---|---|---|---|
| Equality | Compare primitive values, or object references | == | None |
| And | Both operands are true | && | If the left operand is false, skip the right operand |
| Or | At least one operand is true | || | If the left operand is true, skip the right operand |
| Negation | Reverse a Boolean value | ! | None |
Review clarification
Short-circuiting depends on the left operand: Java evaluates these expressions from left to right. Object content equality is covered in Conditionals and equality.
Source: Java Language Specification §15.23–15.24.
De Morgan’s laws
Sets
Java Booleans
For Boolean variables a and b, these equivalent expressions agree:
!(a && b) == (!a || !b)
!(a || b) == (!a && !b)
Syntax correction
Parentheses around the entire right side make each example an equality comparison between the two intended expressions. The original unparenthesized forms were parsed differently because
==binds more tightly than&&and||.
Related: Booleans · Conditionals and equality