# Logic

Source: https://cca.twango.dev/AP-Computer-Science/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 |

> [!note] Review clarification
> Short-circuiting depends on the **left** operand: Java evaluates these expressions from left to right. Object content equality is covered in [[logical-operators|Conditionals and equality]].

Source: [Java Language Specification §15.23–15.24](https://docs.oracle.com/javase/specs/jls/se17/html/jls-15.html#jls-15.23).

## De Morgan's laws

### Sets

$$
\overline{A \cup B} = \overline A \cap \overline B
$$

$$
\overline{A \cap B} = \overline A \cup \overline B
$$

### Java Booleans

For Boolean variables `a` and `b`, these equivalent expressions agree:

```java
!(a && b) == (!a || !b)
!(a || b) == (!a && !b)
```

> [!note] 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: [[variables|Booleans]] · [[logical-operators|Conditionals and equality]]
