Skip to lesson

Comparison operators

A comparison operator compares two values and gives a boolean answer: true or false. For example, marks >= 35 is true when a student has passed.

The six comparison operators

An exam app must tell each student whether they passed: a yes-or-no answer, not another number.

A comparison gives exactly that: marks >= 35 is true for 42 marks and false for 30. These are also called relational operators.

Example 1 · Store the answer in a boolean

Eligibility.javaJava
public class Eligibility {    public static void main(String[] args) {        int attendance = 72;        boolean canSitExam = attendance >= 75;        System.out.println("Can sit exam: " + canSitExam);        double balance = 450.0;        double price = 499.0;        boolean canBuy = balance >= price;        System.out.println("Can buy: " + canBuy);    }}
Output
Can sit exam: false
Can buy: false

What this code does

  • A comparison gives a boolean, from char and boolean (opens in a new tab), so attendance >= 75 can go straight into canSitExam.
  • Comparisons work on double values too: 450.0 is less than 499.0, so canBuy is false.

Java has six of them: == equal to, != not equal to, > greater than, < less than, >= greater than or equal to, and <= less than or equal to. Move the sliders below to try all six.

Experiment

Compare

Move a and b. Make them equal and watch which answers change.

int a = 42;
int b = 35;
a > b
true
a < b
false
a >= b
true
a <= b
false
a == b
false
a != b
true
Write >= and <= with no space, and with = second. => and =< are not Java operators.

== asks, = stores

You want to check for full marks, so you type boolean isFull = (marks = 100);. Java refuses to compile it.

Java
int marks = 100;boolean isFull = (marks = 100);   // error: incompatible types: int cannot be converted to booleanboolean isTop = (marks == 100);   // true

One = stores a value. Two, ==, ask whether two values are equal. marks = 100 asks nothing, so its answer is an int, not a boolean.

Left: marks = 100 puts the value 100 into the variable marks. Right: marks == 100 asks whether marks is 100 and gives the answer true.

Comparing text: use equals()

A login screen checks whether the typed username is "admin". With text, == can say false even when every letter is the same.

To compare the text of two Strings, use equals(): user.equals("admin") gives true or false.

Example 2 · Text: equals() and ==

TextCompare.javaJava
public class TextCompare {    public static void main(String[] args) {        String first = "Ra";        String name = first + "vi";        System.out.println(name);        System.out.println(name.equals("Ravi"));        System.out.println(name == "Ravi");    }}
Output
Ravi
true
false

What this code does

  • name holds the text Ravi, and equals() correctly says true.
  • == says false: name was built while the program ran, so it is a different object from "Ravi".

On a String, == asks a different question: are both variables the very same object in memory? Compare Strings: equals() and compareTo() (opens in a new tab) explains the memory side later in the course.

== is right for numbers, char and boolean. To compare String text, use equals().

Next step

Check your understanding

10 quick questions