Lesson 2.2: Boolean Expressions
Lesson 2.2: Boolean Expressions
Key Vocabulary
| Term | Definition |
|---|---|
| boolean expression | Any expression that evaluates to exactly true or false. The fundamental input to every if statement and loop. (EK 2.2.A.3) |
| relational operator | An operator that compares two values and produces a boolean result. Java has six: ==, !=, <, >, <=, >=. |
| == (equality) | Tests whether two values are equal. With primitives, compares values directly. With objects, compares memory addresses, not content. (EK 2.2.A.1) |
| != (inequality) | Tests whether two values are different. Evaluates to true when operands are not equal, false when they are equal. (EK 2.2.A.1) |
| primitive type | A basic data type stored directly as a value: int, double, boolean, char. Relational operators compare primitive values directly. |
| reference type | A type whose variable stores a memory address pointing to an object, not the object's data directly. Examples: String, ArrayList. Using == on reference types compares addresses. |
| operator precedence | The order in which Java evaluates operators in an expression. Arithmetic operators (*, /, %, then +, -) are evaluated before relational operators (<, >, <=, >=, ==, !=). |
What Is a Boolean Expression?
A boolean expression is any expression that evaluates to exactly one of two values: true or false. This is the most important concept in all of Unit 2 because every selection statement and every loop depends on a boolean expression to determine what runs next. Without boolean expressions, programs cannot make decisions or repeat work.
The boolean data type in Java has exactly two possible values: true and false. These are keywords, not strings — they have no quotes. A boolean variable stores one of these values the same way an int stores a number.
CED Connection (EK 2.2.A.3)
"An expression involving relational operators evaluates to a Boolean value." This is the core of 2.2: learn the six relational operators, understand what each compares, and be able to evaluate any combination of them to true or false.
The Six Relational Operators
Java provides exactly six relational operators. Every AP CSA exam that tests Topic 2.2 uses one or more of these. You need to evaluate them instantly for any numeric values.
| Operator | Meaning & Notes |
|---|---|
| == | Equal to. true when both sides have the same value.⚠ NOT the same as assignment =. Using = inside an if is a compile error in Java.
|
| != | Not equal to. true when both sides have different values. Logical opposite of ==. |
| < | Less than. true when left side is strictly smaller. Does NOT include equal. (EK 2.2.A.2) |
| > | Greater than. true when left side is strictly larger. Does NOT include equal. (EK 2.2.A.2) |
| <= | Less than or equal to. true when left is smaller OR equal. Includes the boundary value. |
| >= | Greater than or equal to. true when left is larger OR equal. Includes the boundary value.⚠ Most common boundary trap: score >= 60 includes 60; score > 60 does not.
|
Equality and Inequality: == and != (EK 2.2.A.1)
The == operator answers the question: "do these two things have the same value?" The != operator answers: "are these two things different?" For primitive types, both operators compare the actual stored values directly and produce an unambiguous result.
Worked Example — == and != with Primitives
int a = 5; int b = 5; int c = 8; System.out.println(a == b); // true — same value System.out.println(a == c); // false — different values System.out.println(a != c); // true — they differ System.out.println(a != b); // false — they are the same boolean sameAge = (a == b); // boolean variable stores the result System.out.println(sameAge); // true
Key point: a relational expression produces a boolean value that can be stored in a variable, passed to a method, or used directly in an if condition.
The critical distinction: primitives vs. reference types (EK 2.2.A.1)
EK 2.2.A.1 explicitly covers both cases. With primitive types (int, double, boolean, char), == compares the actual stored values. With reference types (objects like String, ArrayList), == compares memory addresses — whether two variables point to the same object in memory, not whether their contents are equal.
Worked Example — == on Strings (Reference Trap)
String s1 = new String("hello");
String s2 = new String("hello");
String s3 = s1;
System.out.println(s1 == s2); // false — different objects in memory
System.out.println(s1 == s3); // true — same object (same address)
System.out.println(s1.equals(s2)); // true — same content
s1 and s2 contain identical text but are separate objects. == sees two different addresses and returns false. The correct way to compare String content is .equals(), covered in detail in Lesson 2.6.
AP Trap: = vs. == (Most Common Java Beginner Error)
= is assignment — it stores a value into a variable. == is comparison — it produces a boolean. Writing if (a = 5) in Java is a compile error because a = 5 returns an int, not a boolean. This is intentionally different from C/C++ where it silently compiles and causes bugs. The AP exam exploits this distinction in spot-the-error questions.
Numeric Comparisons: <, >, <=, >= (EK 2.2.A.2)
These four operators establish ordering relationships between numeric values. They apply to int and double variables, literal numbers, and any arithmetic expression that produces a number. The result is always a boolean.
Worked Example — All Six Operators with score = 85
int score = 85; System.out.println(score == 85); // true System.out.println(score != 85); // false System.out.println(score < 90); // true System.out.println(score > 90); // false System.out.println(score <= 85); // true — equal to boundary System.out.println(score >= 90); // false — not equal or above 90 System.out.println(score >= 85); // true — exactly at boundary
The boundary cases (<= and >= when values are exactly equal) are the most frequently tested. Always check: does the problem say "at least", "no more than", "strictly less", or "exceeds"? Each phrase maps to a specific operator.
English-to-operator translation
AP exam questions describe conditions in plain English. You must instantly translate to the correct Java operator. The most frequently confused pairs are > vs. >= and < vs. <=:
| English phrase | Java operator |
|---|---|
| "is equal to" / "same as" | == |
| "is not equal to" / "differs from" | != |
| "is less than" / "is below" / "is under" | < |
| "is greater than" / "is above" / "exceeds" | > |
| "at most" / "no more than" / "does not exceed" | <= |
| "at least" / "no less than" / "minimum of" | >= |
Operator Precedence with Relational Operators
Relational operators have lower precedence than arithmetic operators. This means Java always finishes all arithmetic first, then evaluates the comparison. You do not need parentheses to make a + b > c * 2 work correctly — arithmetic happens first automatically. However, using parentheses for clarity is considered good style.
Worked Example — Precedence in Practice
int a = 3, b = 4; // Arithmetic runs BEFORE relational: System.out.println(a + b > 6); // 3+4=7, then 7>6 = true System.out.println(a * b == 12); // 3*4=12, then 12==12 = true System.out.println(b - a != 0); // 4-3=1, then 1!=0 = true System.out.println(a * 2 <= b); // 3*2=6, then 6<=4 = false // Storing the result: boolean result = a + b >= 7; // 7 >= 7 is true System.out.println(result); // true
Storing and Using Boolean Results
A relational expression produces a boolean value that can be used in three ways: directly inside an if or loop, stored in a boolean variable for reuse, or passed as a method argument. Storing boolean results in named variables often makes code more readable and is a pattern you will use throughout FRQ solutions.
Three Ways to Use a Boolean Expression
int score = 75;
// 1. Directly in an if:
if (score >= 60) {
System.out.println("Passed");
}
// 2. Stored in a variable:
boolean passed = score >= 60;
boolean excellent = score >= 90;
if (passed) {
System.out.println("Passed");
}
// 3. In a while loop condition:
int countdown = 5;
while (countdown > 0) {
System.out.println(countdown);
countdown--;
}
AP Trap: Boundary Values (< vs. <=)
The single most common error on AP MCQ trace questions involving comparisons. score >= 60 is true when score is exactly 60. score > 60 is false when score is exactly 60. Always test boundary values explicitly. If a question gives score = 60 and asks which condition is true, check both options carefully.
AP Trap: Using == to Compare Strings
== on String objects tests memory address equality, not content. Two String objects with identical text may return false for ==. This is tested directly on the AP exam. Always use .equals() to compare String content. This is covered fully in Lesson 2.6.
AP Trap: Chained Comparisons Don't Work in Java
In math, you write 0 < x < 10 to mean "x is between 0 and 10." This does not work in Java. 0 < x < 10 would try to evaluate 0 < x first (producing a boolean), then compare that boolean to 10 — a type error. The correct Java version requires &&: x > 0 && x < 10. This is covered in Topic 2.5.
Real-World Connection
Boolean expressions are the decision engine behind everything a computer does. A credit card processor checks balance >= chargeAmount before approving a transaction. A game engine checks playerHealth > 0 to determine if the game continues. A sensor system checks temperature >= threshold to trigger an alert. Every conditional in software — from a mobile app to a space shuttle controller — is ultimately a boolean expression evaluated to true or false. Topic 2.2 gives you the foundation for writing every condition you will ever use in AP CSA.
Summary
-
Boolean expressions evaluate to
trueorfalse. They are the input to everyifstatement and loop. -
Six relational operators:
==,!=,<,>,<=,>=. All produce boolean results. (EK 2.2.A.2, 2.2.A.3) -
Primitives:
==and!=compare actual values. (EK 2.2.A.1) -
Reference types (objects):
==compares memory addresses, not content. Use.equals()for content. (EK 2.2.A.1) -
Precedence: arithmetic operators evaluate before relational operators.
a + b > ccomputesa + bfirst. -
Boundary trap:
<excludes the boundary;<=includes it. Test boundary values explicitly. -
Chained comparisons like
0 < x < 10do not work in Java. Use&&instead.
Practice Questions
7 != 7?7
false
true
!= evaluates to true only when the values are different. Since 7 equals 7, 7 != 7 evaluates to false.int a = 10; int b = 10;, which expression evaluates to true?a > b
a < b
a != b
a >= b
a >= b is true (10 ≥ 10). A is false (10 > 10), B is false, C is false (they are equal).int temp = 98; boolean isFever = temp > 100; System.out.println(isFever);
false
true
98
100
98 > 100 is false. The boolean variable isFever stores false, which is what gets printed.x == 0
x != y
x + y
x >= y
x + y is an arithmetic expression that evaluates to a numeric value (int or double), not a boolean. All expressions using relational operators (==, !=, <, >, <=, >=) evaluate to boolean.int p = 4; int q = 8; System.out.println(p * 2 == q);
false
true
8
p * 2 = 4 * 2 = 8. Then 8 == 8 = true. Arithmetic is evaluated before relational operators.int n = 15; System.out.println(n % 2 == 0); System.out.println(n > 10);
falsetrue
truefalse
truetrue
falsefalse
15 % 2 = 1, and 1 == 0 is false. Then 15 > 10 is true.boolean variable that is true when score is at least 60?boolean pass = score;
int pass = score >= 60;
boolean pass = (score >= 60) = true;
boolean pass = score >= 60;
score >= 60 evaluates to a boolean, which can be assigned directly to a boolean variable. A assigns an int to boolean (type error). B assigns boolean to int (type error). C uses = on an expression (compile error).I.
a == b and b == aII.
a < b and b > aIII.
a <= b and a < b
a == b and b == a are always equivalent (equality is symmetric). II: a < b and b > a are always equivalent. III: a <= b is true when a == b, but a < b is false when a == b — not equivalent.Mastery: Boolean Expression Analysis
int x = 10; int y = 20; boolean result = x + 5 > y - 3; System.out.println(result);
true
15
17
false
10 + 5 = 15. Right side: 20 - 3 = 17. 15 > 17 is false.boolean inRange = (1 <= x) == (x <= 10); to check if x is between 1 and 10. When x = 5, this evaluates to true. When x = 0, what happens?inRange is true, correctly identifying 0 as out of rangeinRange is true even though 0 is out of range — a logic errorinRange is false, correctly identifying 0 as out of range== on boolean expressions1 <= 0 is false, 0 <= 10 is true. So false == true is false... wait — actually let’s re-trace. When x = 15: 1 <= 15 is true, 15 <= 10 is false, true == false = false. When x = 0: false == true = false. The trap is x = -5: false == false = true — incorrectly says -5 is in range. The expression is a logic error for negative values below 1.grade is exactly 100?if (grade = 100)
if (grade >= 100 <= 100)
if (grade == 100)
if (grade =! 100)
= (assignment), which is a compile error in an if condition. B chains relational operators incorrectly (Java does not support that syntax). D uses =! which is not a valid operator (you want !=). Only C uses == correctly.int a = 6; int b = 3; System.out.println(a / b == 2); System.out.println(a % b == 0);
truetrue
truefalse
falsetrue
falsefalse
6 / 3 = 2 (integer division), so 2 == 2 = true. 6 % 3 = 0, so 0 == 0 = true. Both lines print true.Boolean Variables as Flags
Experienced programmers often use flag variables — boolean variables that track state throughout a program. For example: boolean found = false; at the start of a search, then set to true when the item is located. How does storing a boolean expression in a named variable improve code readability compared to putting the expression directly in an if statement?
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]