01 / 21
\(<\)
\(>\)

Comparison
Operators

Learn how Java's six comparison operators turn values into boolean expressions: the foundation of every decision your programs make.

Java

What Is a Boolean?

In Java, a boolean is a data type that holds exactly one of two possible values: true or false. Every comparison operator produces a boolean result.

true false or

What Are Comparison Operators?

A comparison operator compares two values and returns a boolean result. Java provides six comparison operators, also called relational operators.

Equality
\(==\) and \(!=\) check if two values are equal or not equal.
Ordering
\(<\), \(>\), \(<=\), \(>=\) compare magnitude.

Why Comparisons Matter

Almost every program makes decisions. Comparison operators produce the boolean expressions that drive if, while, and for statements.

\(\text{score} >= 60\)
boolean? true Pass false Fail A comparison operator feeds a boolean into a decision

The Six Comparison Operators

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

Anatomy of a Boolean Expression

x \(>=\) 10
Left operand Operator Right operand bool

\(==\) vs \(=\) : The Key Distinction

\(=\) Assignment \(==\) Comparison
PurposeStores a valueTests equality
Example\(\text{x} = 5;\)\(\text{x} == 5\)
Result typeThe assigned valueboolean
Used inDeclarations, assignmentsif, while, for

How to Form a Boolean Expression

1
Identify what you are comparing. Are you comparing two variables, a variable and a literal, or two expressions?
2
Choose the correct operator. Do you need equality (\(==\), \(!=\)) or ordering (\(<\), \(>\), \(<=\), \(>=\))?
3
Write the expression. Place the left operand, operator, and right operand in order.
4
Evaluate mentally. Substitute known values and verify: does the result match your expectation of true or false?

Example 1: Simple Comparisons

int a = 7; int b = 12; boolean result1 = a < b; // true (7 < 12) boolean result2 = a == b; // false (7 != 12) boolean result3 = a >= 7; // true (7 >= 7) boolean result4 = b != 12; // false (12 == 12)

Example 2: Inside an If Statement

int temperature = 98; if (temperature >= 100) { System.out.println("Boiling!"); } else { System.out.println("Not boiling yet."); } // Output: Not boiling yet.

Example 3: Comparing Expressions

int x = 4; int y = 9; boolean check = (x * x) < y; // (4 * 4) < 9 → 16 < 9 → false boolean check2 = (x + 1) == (y - 4); // (4 + 1) == (9 - 4) → 5 == 5 → true

Comparing Doubles: Precision Pitfall

double a = 0.1 + 0.2; boolean result = (a == 0.3); // false! a is actually 0.30000000000000004

Comparing Characters

Java compares char values using their Unicode (ASCII) numbers. For example, 'A' is 65 and 'a' is 97.

char letter = 'B'; boolean r1 = letter == 'B'; // true boolean r2 = letter < 'a'; // true (66 < 97) boolean r3 = 'Z' > 'A'; // true (90 > 65)

Operator Precedence

Comparison operators have lower precedence than arithmetic operators. Java evaluates math first, then comparisons.

1
Parentheses \(()\) : highest priority
2
Arithmetic \(*, /, \%, +, -\)
3
Comparison \(<, >, <=, >=\) then \(==, !=\)
4
Logical \(\&\&, ||\) : lowest

Common Mistakes

✕ Using = instead of ==
\(\text{if (x = 5)}\) assigns 5 to x. Use \(\text{if (x == 5)}\) to compare.
✕ Using == on Strings
\(\text{"hi"} == \text{"hi"}\) compares references, not content. Use .equals() for Strings.
✕ Confusing \(<\) and \(<=\)
\(x < 5\) excludes 5 but \(x <= 5\) includes it. Off-by-one errors are common.

Summary Reference

Operator Name True When Watch Out
\(==\)Equal toValues are the sameNot for Strings
\(!=\)Not equal toValues differNot for Strings
\(<\)Less than\(\text{Left} < \text{Right}\)Excludes equal
\(>\)Greater than\(\text{Left} > \text{Right}\)Excludes equal
\(<=\)Less or equal\(\text{Left} <= \text{Right}\)Includes equal
\(>=\)Greater or equal\(\text{Left} >= \text{Right}\)Includes equal

Question 1 of 5

What is the value of the following expression?
\(10 > 10\)

Question 2 of 5

Given int x = 3; and int y = 8;, what does \((x + y) == 11\) evaluate to?

Question 3 of 5

Which operator should you use to check if a variable score is at least 90?

Question 4 of 5

What is the output of this code?

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

Question 5 of 5

Which of the following is a valid boolean expression in Java?