Comparison
Operators
Learn how Java's six comparison operators turn values into boolean expressions: the foundation of every decision your programs make.
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.
What Are Comparison Operators?
A comparison operator compares two values and returns a boolean result. Java provides six comparison operators, also called relational operators.
Why Comparisons Matter
Almost every program makes decisions. Comparison operators produce the boolean expressions that drive if, while, and for statements.
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
\(==\) vs \(=\) : The Key Distinction
| \(=\) Assignment | \(==\) Comparison | |
|---|---|---|
| Purpose | Stores a value | Tests equality |
| Example | \(\text{x} = 5;\) | \(\text{x} == 5\) |
| Result type | The assigned value | boolean |
| Used in | Declarations, assignments | if, while, for |
How to Form a Boolean Expression
true or false?Example 1: Simple Comparisons
Example 2: Inside an If Statement
Example 3: Comparing Expressions
Comparing Doubles: Precision Pitfall
Comparing Characters
Java compares char values using their Unicode (ASCII) numbers. For example, 'A' is 65 and 'a' is 97.
Operator Precedence
Comparison operators have lower precedence than arithmetic operators. Java evaluates math first, then comparisons.
Common Mistakes
.equals() for Strings.Summary Reference
| Operator | Name | True When | Watch Out |
|---|---|---|---|
| \(==\) | Equal to | Values are the same | Not for Strings |
| \(!=\) | Not equal to | Values differ | Not 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\)
false. To include the equal case, use \(>=\).Question 2 of 5
Given int x = 3; and int y = 8;, what does \((x + y) == 11\) evaluate to?
true. Comparison operators always return a boolean, not a number.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?
n is 5, they are equal, so the "not equal" test returns false.Question 5 of 5
Which of the following is a valid boolean expression in Java?
true or false. \(5 + 3\) is an arithmetic expression (returns int), \(\text{x} = 10\) is assignment, and "hello" is a String literal. None of those are boolean.