1s
Need help with this lesson?
📚AP Computer Science A•easy• 8 min

Boolean Expressions and Relational Operators

Construct and evaluate boolean expressions using relational operators to control if statements and loop conditions accurately.

A boolean expression is anything that evaluates to true or false. Relational operators compare two values and produce a boolean. They are the raw material every if statement and loop condition is built from.

The six relational operators

OperatorMeaningExampleResult
==equal to5 == 5true
!=not equal to5 != 3true
< >less / greater than3 > 7false
<= >=less / greater than or equal4 <= 4true

Each comparison produces a boolean value.

Storing a comparison in a boolean

The result of a comparison is a value you can store, print, or test.

int score = 72;
boolean passing = score >= 65;
System.out.println(passing);

Output:

true

Comparing numbers, not objects

For the primitive types int and double, == compares values — exactly what you expect. (Remember from Unit 1: for objects such as String, == compares references, so use .equals for String contents.)

A single = means assignment; a double == means comparison. Mixing them up is the most common beginner bug.

Worked example 1

Evaluate each comparison.

System.out.println(8 > 5);
System.out.println(8 == 5);
System.out.println(8 != 5);
System.out.println(5 <= 5);

Output:

true
false
true
true

Each line prints the boolean result. Note 5 <= 5 is true because “or equal” is included.

Worked example 2

Comparisons can include arithmetic.

int a = 3, b = 4;
System.out.println(a + b > 6);
System.out.println(a * b == 12);

Output:

true
true

Arithmetic happens first (higher precedence than relational operators): 3 + 4 = 7 > 6 → true; 3 * 4 = 12 == 12 → true.

Worked example 3

Store and reuse a boolean.

int age = 20;
boolean isAdult = age >= 18;
boolean isTeen = age >= 13 && age <= 19;
System.out.println(isAdult);
System.out.println(isTeen);

Output:

true
false

isAdult is true (20 ≥ 18). isTeen is false because 20 is not ≤ 19.

Worked example 4

== on Strings vs. numbers.

String a = "hi", b = "hi";
System.out.println(a.equals(b));
int x = 4, y = 4;
System.out.println(x == y);

Output:

true
true

Compare String contents with .equals (true). For primitives, == compares values directly (4 == 4 → true).

Worked example 5

Negating a comparison.

int n = 10;
System.out.println(!(n > 5));

Output:

false

n > 5 is true; ! flips it to false. (More on ! in the next lesson.)

Common trap: = is not ==

= stores a value; == asks a question. In Java, writing if (x = 5) does not even compile, because x = 5 produces an int, not a boolean. Always use == inside conditions.

Hack: Read == as a question

When you see ==, read it out loud as “is equal to?” and = as “becomes.” x == 5 asks a question (gives a boolean); x = 5 gives a command (stores 5). If a condition ever fails to compile with a type error, check for a stray single = first.

And keep Unit 1 in mind: numbers use ==, Strings use .equals.

Print companion
AP Computer Science A Power Workbook
Off-screen practice on Amazon