01 / 20
&&

Boolean
Expressions

Master the building blocks of decision-making in Java: logical operators, comparison operators, and how to combine them into compound conditions.

AP Computer Science A

What Is a Boolean?

In Java, a boolean is a primitive data type that holds exactly one of two values: true or false. Every decision your program makes: if statements, while loops, and conditionals: relies on boolean expressions that evaluate to one of these values.

true
The condition is satisfied. The code block executes.
false
The condition is not met. The code block is skipped.

Relational Operators

Relational (comparison) operators compare two values and return a boolean result. These are the foundation of every boolean expression.

Operator Meaning Example Result
==Equal to5 == 5true
!=Not equal to5 != 3true
<Less than\(3 < 5\)true
>Greater than\(3 > 5\)false
<=Less than or equal\(5 \le 5\)true
>=Greater than or equal\(4 \ge 7\)false

Logical Operators

Logical operators let you combine multiple boolean expressions into a single compound expression. Java has three logical operators:

&& (AND)
true only when both sides are true
|| (OR)
true when at least one side is true
! (NOT)
Flips the value: true becomes false, false becomes true

Truth Tables for AND, OR, NOT

&& (AND)
A B
\(A \land B\)
true true true true false false false true false false false false
|| (OR)
A B
\(A \lor B\)
true true true true false true false true true false false false
! (NOT)
A
\(\neg A\)
true false false true Quick Mnemonics
&& : "Both must be true"
|| : "At least one must be true"
! : "Flip it"

Short-Circuit Evaluation

Java uses short-circuit evaluation: it stops evaluating a compound boolean expression as soon as the result is determined.

&& Short-Circuit
If the left side is false, the right side is never evaluated because the whole expression must be false.
|| Short-Circuit
If the left side is true, the right side is never evaluated because the whole expression must be true.

== vs .equals()

This is one of the most important distinctions in Java. The == operator and the .equals() method compare different things.

== Operator .equals() Method
Compares References (memory addresses) Content (values)
Use for Primitives (int, double, boolean) Objects (String, etc.)
"hello" == "hello" May be true (string pool) true (always reliable)
new String("hi") == new String("hi") false true

De Morgan's Laws

De Morgan's Laws describe how to distribute a NOT operator (!) across compound boolean expressions. They are essential for simplifying and understanding negated conditions.

Law 1
!(A && B) is equivalent to !A || !B
Law 2
!(A || B) is equivalent to !A && !B

Visualizing De Morgan's

Law 1
!(A && B)
→
!A || !B
Law 2
!(A || B)
→
!A && !B

How to Evaluate Boolean Expressions

1
Identify the operator precedence. Parentheses first, then !, then &&, then ||.
2
Evaluate innermost parentheses first. Substitute known values for variables.
3
Apply NOT operators (!). Flip true to false and false to true.
4
Evaluate AND (&&) operators. Both sides must be true for the result to be true.
5
Evaluate OR (||) operators. At least one side must be true.
6
Check for short-circuit evaluation. Did the left side already determine the result?

Example 1: Simple Expression

int x = 7; int y = 3; boolean result = (x > 5) && (y < 10);
1
Evaluate (x > 5): \(7 > 5\) is true
2
Evaluate (y < 10): \(3 < 10\) is true
3
Combine with &&: true && true = true

Example 2: Compound Expression

int age = 16; boolean hasLicense = false; boolean canDrive = (age >= 16) && hasLicense;
1
Evaluate (age >= 16): \(16 \ge 16\) is true
2
Evaluate hasLicense: false
3
Combine with &&: true && false = false

Example 3: Applying De Morgan's

Simplify: !(x > 5 && y < 3) given \(x = 4\), \(y = 7\)

1
Apply De Morgan's Law 1: !(A && B) becomes !A || !B
2
Result: !(x > 5) || !(y < 3) which is (x <= 5) || (y >= 3)
3
Evaluate: \(4 \le 5\) is true, \(7 \ge 3\) is true
4
true || true = true

Common Mistakes

✕ Using = instead of ==
A single = is assignment, not comparison. Writing if (x = 5) assigns 5 to x. Use == to compare.
✕ Using == for Strings
Comparing Strings with == checks memory addresses, not content. Always use .equals() for String comparison.
✕ Ignoring Precedence
Writing a || b && c evaluates && first. If you mean (a || b) && c, you must use parentheses.
✕ Wrong De Morgan's
Forgetting to flip the operator when distributing NOT. !(a && b) is NOT the same as !a && !b: the && must flip to ||.
✕ Null Reference
Calling .equals() on a null object throws an error. Check for null first: str != null && str.equals("hi").

Boolean Cheat Sheet

Concept Syntax Key Rule
AND&&Both must be true
OR||At least one true
NOT!Flips the value
Equality (primitives)==Compares values
Equality (objects).equals()Compares content
De Morgan's 1!(A && B)!A || !B
De Morgan's 2!(A || B)!A && !B
Short-circuit &&false && ...Right side skipped
Short-circuit ||true || ...Right side skipped

Question 1 of 5

What does the following expression evaluate to?
!(true && false)

Question 2 of 5

Given int x = 0, what happens when Java evaluates:
x != 0 && (10 / x > 2)

Question 3 of 5

Which is the correct De Morgan's equivalent of !(a > 5 || b < 3)?

Question 4 of 5

What is the value of result after this code runs?

Question 5 of 5

Given boolean a = true, b = false, c = true, what does this evaluate to?
a || b && c