Learn Java
import com.learnjava.operators.OpComparison;

Comparison Operators in Java

@Concept

Java's six comparison operators — ==, !=, <, >, <=, >= — each evaluate to a boolean, making them the building blocks of every conditional and loop. For primitives, == and != compare actual values directly, exactly as expected.

For objects, including String, == and != compare references — whether two variables point to the same object in memory — not content. That's a common early source of confusion, especially since two separately-created strings holding identical text often still fail an == check. The equals method, not ==, is what actually compares content.

@HowItWorks
1

The six comparison operators

==, !=, <, >, <=, >= each produce a boolean result.

2

Comparing primitives

For numbers, == and != check the actual numeric values directly.

3

Comparing objects

For anything that isn't a primitive, == and != compare references — are these the same object? — not content.

4

Comparing content instead

The equals method, inherited from Object and overridden by classes like String, is what actually compares content rather than identity.

5

Ordering only works on numbers and char

<, >, <=, >= are defined for numeric types and char, but not for objects like String, which need compareTo instead.

@Example
public class ComparisonDemo {
public static void main(String[] args) {
int a = 5, b = 10;
String s1 = new String("hi");
String s2 = new String("hi");
System.out.println("a < b: " + (a < b));
System.out.println("s1 == s2: " + (s1 == s2));
System.out.println("s1.equals(s2): " + s1.equals(s2));
}
}
Output
a < b: true
s1 == s2: false
s1.equals(s2): true
1a < b compares two primitive ints directly, correctly returning true.
2s1 and s2 were each built with new String(...), so they're two separate objects — s1 == s2 checks reference identity, finds different objects, and returns false.
3s1.equals(s2) compares the actual characters inside each String, which match, so it returns true.
4All three println calls run once each, making the reference-versus-content distinction visible side by side.
@UseCases
Writing the condition inside an if statement or while loop
Debugging a false result from == on two objects that clearly look the same
Sorting or searching, which typically compares with < / > on numbers or compareTo on objects
Knowing when to reach for equals instead of == when checking two objects for the same content

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.