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
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | equal | 5 == 5 | true |
!= | not equal | 5 != 3 | true |
> | greater than | 5 > 3 | true |
< | less than | 3 < 5 | true |
>= | greater or equal | 5 >= 5 | true |
<= | less or equal | 5 <= 5 | true |
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== 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 roundingThe 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 contentsThe 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 itThe == "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)); // trueComparing 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)); // positiveInteger, Long, String, LocalDate, and most "natural" types implement Comparable. Custom ordering uses Comparator — covered in Comparable and Comparator.
A demonstration
What's next
Java Logical Operators — combining boolean expressions with &&, ||, and !.
Practice
How should you compare two String objects for content equality in Java?