🌱BeginnerOperators

Comparison Operators

Compare values in Java using ==, !=, >, <, >=, and <= - operators that return a boolean result.

Overview

Comparison operators compare two values and return a boolean result (true or false). They are the foundation of all decision-making in programs - every if statement, loop condition, and ternary expression uses them. Java has six comparison operators. They work naturally on primitive types. For objects (including Strings), == compares references (memory addresses), not content - use .equals() for content comparison.

Key Points

  • •== equal to (do not confuse with = assignment)
  • •!= not equal to
  • •> greater than, < less than
  • •>= greater than or equal to, <= less than or equal to
  • •All comparison operators return boolean: true or false
  • •For objects use .equals() - == on objects checks if they are the same object in memory, not equal content

Examples

All six comparison operators

public class Main {
    public static void main(String[] args) {
        int a = 10, b = 20;

        System.out.println(a == b);  // false
        System.out.println(a != b);  // true
        System.out.println(a > b);   // false
        System.out.println(a < b);   // true
        System.out.println(a >= 10); // true  (equal counts)
        System.out.println(b <= 15); // false
    }
}

The result is always true or false. These results are often used directly in if conditions.

Comparing in conditions

public class Main {
    public static void main(String[] args) {
        int score = 75;

        if (score >= 90) {
            System.out.println("Grade: A");
        } else if (score >= 70) {
            System.out.println("Grade: B");
        } else {
            System.out.println("Grade: C or below");
        }

        int age = 17;
        System.out.println("Can vote: " + (age >= 18)); // false
    }
}

You can print a comparison result directly: System.out.println(age >= 18) prints false.

== on primitives vs objects

public class Main {
    public static void main(String[] args) {
        // Primitives: == compares values
        int x = 5, y = 5;
        System.out.println(x == y); // true ✓

        // Strings: == compares references (memory address)
        String s1 = new String("hello");
        String s2 = new String("hello");
        System.out.println(s1 == s2);      // false ✗ (different objects)
        System.out.println(s1.equals(s2)); // true  ✓ (same content)

        // String literals may be the same object (string pool)
        String a = "hi";
        String b = "hi";
        System.out.println(a == b); // true (but don't rely on this!)
    }
}

Always use .equals() for String (and object) comparison to avoid subtle bugs.

Common Mistakes

  • Writing = (assignment) instead of == (comparison) inside an if condition - Java will give a compile error for non-boolean assignment in if
  • Using == to compare Strings - this compares object references, not content; use .equals()
  • Confusing >= with => (there is no => comparison in Java; => is only for lambda expressions)

Note

Store comparison results in a boolean variable for clarity: `boolean isAdult = age >= 18;`. This makes later code that uses the result more readable.

Exercises

Write Java code and click Run to execute it instantly. When your output looks correct, mark as done.

Loading editor…

Completed: 0 / 4 exercises

Knowledge Check

Question 1 of 4

What does the expression `10 != 10` evaluate to?