CCA Guide
Logic

Logic

Curated by Last modified: 1 min read

View Markdown

Logical operators

NameMeaningJava symbolShort-circuit behavior
EqualityCompare primitive values, or object references==None
AndBoth operands are true&&If the left operand is false, skip the right operand
OrAt least one operand is true||If the left operand is true, skip the right operand
NegationReverse 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

Linked to this page