Topic 2.2 Notes – Boolean Expressions
1. What Boolean Expressions Are
A Boolean expression is any expression that evaluates to either true or false.
In this unit, that usually means:
- You compare two values using a relational operator
- The result of that comparison is a boolean
Example:
int temperature = 72;
boolean isHot = temperature > 80; // false
temperature > 80 is the Boolean expression.
It evaluates to false, and that value gets stored in isHot.
You’ll see Boolean expressions:
- Inside
if (...) - Inside
while (...) - Assigned directly to
booleanvariables
If it doesn’t evaluate to true or false, it’s not a Boolean expression.
2. Relational Operators
There are six relational operators you need to know cold:
| Operator | Meaning |
|---|---|
== | equal to |
!= | not equal to |
< | less than |
> | greater than |
<= | less than or equal to |
>= | greater than or equal to |
Every one of these produces a boolean result.
Comparing Primitive Types
With primitive types (int, double, char, boolean), comparisons are straightforward. You are comparing the actual stored values.
int x = 4;
int y = 9;
x < y // true
x == y // false
Important details:
- Numeric types (
int,double) can use all six operators. booleanvariables can only use==and!=.
boolean a = true;
boolean b = false;
a == b // false
The result is always true or false. Never a number.
Comparing Reference Types
Reference types store memory addresses, not raw data. That changes what == means.
String s1 = new String("hello");
String s2 = new String("hello");
s1 == s2 // false
Even though the text is the same, these are two different objects in memory.
This diagram shows the same idea using the String pool. Notice how two variables can point to the same object, or to different objects with different contents.
String pool and reference comparison
With reference types:
==checks if two variables refer to the same object- It does not compare contents
On AP CSA, assume == compares references unless you're explicitly told otherwise.
3. How Boolean Expressions Evaluate
Any relational expression evaluates to true or false.
7 >= 7 // true
10 != 3 // true
5 > 12 // false
When arithmetic is involved, Java evaluates the math first.
int a = 3;
int b = 6;
a * 2 <= b + 1
Step-by-step:
a * 2 → 6
b + 1 → 7
6 <= 7 → true
Arithmetic happens first.
Then the relational operator compares the results.
Then you get a boolean.
On multiple-choice questions, slow down and evaluate each side fully before comparing.
4. Using Boolean Expressions in Code
Inside an if statement
if (score >= 75) {
System.out.println("Passed");
}
The condition must evaluate to a boolean. Nothing else works there.
Inside a while loop
int count = 0;
while (count < 5) {
count++;
}
The loop continues as long as the Boolean expression is true.
Assigning Boolean Results
You’ll often see this on FRQs:
boolean isEven = number % 2 == 0;
That entire comparison becomes the stored value.
Students sometimes overcomplicate this by writing:
if (number % 2 == 0) {
isEven = true;
} else {
isEven = false;
}
You don’t need that. The comparison already gives you a boolean.
5. Common Mistakes That Show Up on Tests
1. Using = instead of ==
if (x = 5) // error
= assigns.
== compares.
This is one of the most common quiz mistakes.
2. Confusing Reference and Primitive Comparison
If two objects have the same content, that does not guarantee == returns true.
That misunderstanding shows up often in code tracing problems.
3. Off-by-One Errors
These are everywhere in loop conditions.
i < arr.length // correct
i <= arr.length // runtime error
That single extra equals sign can cause an exception.
Pay close attention to < versus <=. The AP loves boundary errors.