Understanding Java Comparison Operators: Equal, Greater, Less and More
Comparison operators in Java compare two values and always produce a boolean result: true or false. They are most often used in conditions that control the if statement and loop statements such as while and for.
Comparison Operators Table
Java has six comparison operators. Two of them test equality, and four test ordering:
| Operator | Description | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 5 | true |
| != | Not equal to | 5 != 5 | false |
| > | Greater than | 4 > 1 | true |
| < | Less than | 4 < 1 | false |
| >= | Greater than or equal to | 3 >= 3 | true |
| <= | Less than or equal to | 2 <= 1 | false |
Using Comparison Operators in Conditions and Loops
The result of a comparison can be stored in a boolean variable or used directly as a condition:
int a = 4;
int b = 1;
boolean result = a < b;
System.out.println(result); // false
In practice, comparisons usually appear inside if statements and loop conditions:
int temperature = 25;
if (temperature >= 20) {
System.out.println("Warm enough");
}
for (int i = 0; i < 3; i++) {
System.out.println(i); // prints 0, 1, 2
}
Equality Operators: == and !=
The == and != operators work with three kinds of operand pairs:
- Numbers and char: the values are compared numerically. Operands of different numeric types are first promoted to a common type, so
5 == 5.0istrue. - Booleans: two
booleanvalues can be compared with each other, but not with numbers —true == 1does not compile in Java. - References: for objects,
==checks whether two references point to the same object in memory, not whether the objects have equal content.
String s1 = new String("Java");
String s2 = new String("Java");
System.out.println(s1 == s2); // false: two different objects
System.out.println(s1.equals(s2)); // true: the same characters
Important
Use == only for primitive values. To compare the content of objects — strings in particular — always call the equals() method. Comparing objects with == is one of the most common Java beginner bugs.
Ordering Operators: >, <, >=, <=
The ordering operators >, <, >=, <= are allowed only for primitive numeric types: byte, short, int, long, float, double and char. Characters are compared by their numeric (Unicode) values:
char letter = 'B';
System.out.println(letter > 'A'); // true: 66 > 65
You cannot apply ordering operators to boolean values or to objects. To order strings or other objects, use the compareTo() method instead:
String x = "apple";
String y = "banana";
System.out.println(x.compareTo(y) < 0); // true: "apple" comes first
Common Pitfalls
Three comparisons regularly surprise Java developers:
// 1. Floating-point values are not stored exactly
System.out.println(0.1 + 0.2 == 0.3); // false
// 2. NaN is not equal to anything, including itself
System.out.println(Double.NaN == Double.NaN); // false
// 3. Integer wrapper caching
Integer i1 = 127, i2 = 127;
Integer j1 = 128, j2 = 128;
System.out.println(i1 == i2); // true: cached objects
System.out.println(j1 == j2); // false: different objects
For floating-point values compare with a small tolerance (epsilon) or use BigDecimal; for Double.NaN use Double.isNaN(); for wrapper classes such as Integer use equals().
Frequently Asked Questions
What is the difference between == and equals() in Java?
For primitive types == compares values. For objects == compares references, i.e. whether both variables point to the same object, while equals() compares the logical content of the objects. That is why two different String objects with the same text are equal by equals() but not by ==.
Can I compare strings or other objects with < and > in Java?
No. The ordering operators work only with primitive numeric types and char. Applying them to objects is a compile-time error. To order strings, use compareTo(): a negative result means the first string comes earlier lexicographically.
Why does 0.1 + 0.2 == 0.3 return false in Java?
Because double stores numbers in binary form, 0.1 and 0.2 cannot be represented exactly, and the sum is 0.30000000000000004. Compare floating-point values with a small tolerance, for example Math.abs(a - b) < 1e-9, or use BigDecimal for exact arithmetic.
Why does == return false for two equal Integer values such as 128?
Autoboxing reuses cached Integer objects only for values from minus 128 to 127. Inside that range == happens to return true because both references point to the same cached object; outside it, two distinct objects are created and == returns false. Always compare wrapper objects with equals().
Comments