W3docs

Java Comparison Operators

Compare values in Java with ==, !=, >, <, >=, and <=, and learn when to use .equals() instead of == for objects.

Comparison operators take two operands and produce a boolean. Java has six of them — equal, not-equal, and the four relational operators. The catch is that == does something different on primitives than on objects, which is the single most common source of beginner bugs.

The six operators

OperatorMeaningExampleResult
==equal5 == 5true
!=not equal5 != 3true
>greater than5 > 3true
<less than3 < 5true
>=greater or equal5 >= 5true
<=less or equal5 <= 5true

Each yields a boolean you can use in an if test, a loop condition, or as the value of a variable:

int score = 85;
boolean passed = score >= 60;        // true
boolean perfect = score == 100;      // false
boolean failed = !(score >= 60);     // false

The relational operators (>, <, >=, <=) are most common as if conditions and loop guards:

int temperature = 30;

if (temperature > 25) {
  System.out.println("warm");        // printed
} else if (temperature < 10) {
  System.out.println("cold");
}

for (int i = 0; i < 3; i++) {        // i < 3 is the loop guard
  System.out.println(i);             // 0 1 2
}
Warning

= is assignment, == is comparison. Writing if (x = 5) instead of if (x == 5) is the classic beginner bug. In Java it usually fails to compile (an int is not a boolean), but with boolean variables if (done = true) does compile and silently assigns instead of comparing. Always use == to test for equality.

== on primitives

For primitive types, == compares the value:

int a = 5;
int b = 5;
System.out.println(a == b);    // true

double x = 0.1 + 0.2;
double y = 0.3;
System.out.println(x == y);    // false — IEEE 754 rounding

The floating-point case is a reminder: don't compare double values for exact equality. Test that they're within a small tolerance:

System.out.println(Math.abs(x - y) < 1e-9);   // true

== on objects — identity, not equality

For reference types, == compares references — whether the two variables point at the same object in memory. Two distinct objects with identical contents are not ==:

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 contents

The rule of thumb: for objects, always use .equals() to compare contents.

if (input.equals("quit")) { ... }      // correct
// if (input == "quit") { ... }        // works by coincidence, don't rely on it

The == "quit" form sometimes returns true because Java de-duplicates string literals — the same literal compiles to the same object. But that's an implementation detail, not a guarantee.

Objects.equals — null-safe

If either side might be null, obj.equals(other) throws NullPointerException. Use Objects.equals instead — it handles null on either side:

import java.util.Objects;

String a = null;
String b = "hello";

System.out.println(Objects.equals(a, b));   // false (no NPE)
System.out.println(Objects.equals(a, null)); // true

Comparing wrapper objects

Wrapper types (Integer, Long, etc.) follow the object rule — == compares references, not values:

Integer x = 200;
Integer y = 200;
System.out.println(x == y);         // false (usually)
System.out.println(x.equals(y));    // true

(Values from -128 to 127 are cached, so Integer x = 100; Integer y = 100; x == y happens to be true — but that's a JVM quirk, not the language rule. Don't rely on it.)

Comparing for ordering — .compareTo

<, >, <=, >= only work on primitives. To compare objects for ordering, use the Comparable.compareTo method (returns negative, zero, or positive):

String a = "apple";
String b = "banana";

System.out.println(a.compareTo(b));   // negative — "apple" < "banana"
System.out.println(a.compareTo(a));   // 0
System.out.println(b.compareTo(a));   // positive

Integer, Long, String, LocalDate, and most "natural" types implement Comparable. Custom ordering uses Comparator — covered in Comparable and Comparator.

A demonstration

java— editable, runs on the server

What's next

Java Logical Operators — combining boolean expressions with &&, ||, and !.

Practice

Practice
How should you compare two String objects for content equality in Java?
How should you compare two String objects for content equality in Java?
Was this page helpful?