Boolean
Expressions
Master the building blocks of decision-making in Java: logical operators, comparison operators, and how to combine them into compound conditions.
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.
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 to | 5 == 5 | true |
!= | Not equal to | 5 != 3 | true |
< | 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)|| (OR)! (NOT)Truth Tables for AND, OR, NOT
Short-Circuit Evaluation
Java uses short-circuit evaluation: it stops evaluating a compound boolean expression as soon as the result is determined.
&& Short-Circuit|| Short-Circuit== 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.
!(A && B) is equivalent to !A || !B!(A || B) is equivalent to !A && !BVisualizing De Morgan's
How to Evaluate Boolean Expressions
!, then &&, then ||.!). Flip true to false and false to true.&&) operators. Both sides must be true for the result to be true.||) operators. At least one side must be true.Example 1: Simple Expression
int x = 7;
int y = 3;
boolean result = (x > 5) && (y < 10);(x > 5): \(7 > 5\) is true(y < 10): \(3 < 10\) is true&&: true && true = trueExample 2: Compound Expression
int age = 16;
boolean hasLicense = false;
boolean canDrive = (age >= 16) && hasLicense;(age >= 16): \(16 \ge 16\) is true&&: true && false = falseExample 3: Applying De Morgan's
Simplify: !(x > 5 && y < 3) given \(x = 4\), \(y = 7\)
!(A && B) becomes !A || !B!(x > 5) || !(y < 3) which is (x <= 5) || (y >= 3)true || true = trueCommon Mistakes
= instead of === is assignment, not comparison. Writing if (x = 5) assigns 5 to x. Use == to compare.== for Strings== checks memory addresses, not content. Always use .equals() for String comparison.a || b && c evaluates && first. If you mean (a || b) && c, you must use parentheses.!(a && b) is NOT the same as !a && !b: the && must flip to ||..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)
true && false evaluates to false (both must be true for AND). Then ! flips it to true.Question 2 of 5
Given int x = 0, what happens when Java evaluates:x != 0 && (10 / x > 2)
x != 0 is false, short-circuit evaluation means && immediately returns false without evaluating the division. No error occurs.Question 3 of 5
Which is the correct De Morgan's equivalent of !(a > 5 || b < 3)?
!(A || B) becomes !A && !B. The || flips to &&, and each comparison is negated: > becomes <= and < becomes >=.Question 4 of 5
What is the value of result after this code runs?
String s1 = new String("hello");
String s2 = new String("hello");
boolean result = (s1 == s2);
== operator compares references, not content. Since new String() creates two separate objects in memory, they have different references, so the result is false. Use .equals() for content comparison.Question 5 of 5
Given boolean a = true, b = false, c = true, what does this evaluate to?a || b && c
&& has higher precedence than ||, so this is evaluated as a || (b && c). First: false && true = false. Then: true || false = true.