Equality operators are among the most frequently used operators in Java. They allow us to compare two values and determine whether they are equal or not.
Unlike relational operators (<, >, <=, >=), equality operators work with all primitive data types, including boolean, and they can also compare object references.
However, many beginners get confused about how == behaves with objects, strings, and null. These concepts are also some of the most frequently asked Java interview questions.
Let's understand them with simple explanations and practical examples.
What Are Equality Operators?
Java provides two equality operators.
| Operator | Description |
|---|---|
== |
Equal to |
!= |
Not equal to |
Both operators always return a boolean value.
Example
System.out.println(10 == 10);
System.out.println(20 != 10);
System.out.println(5 == 8);
Output
true
true
false
Rule 1: Equality Operators Work with All Primitive Types
Unlike relational operators, equality operators can be applied to every primitive type, including boolean.
Supported primitive types include:
byteshortintlongfloatdoublecharboolean
Numeric Examples
System.out.println(10 == 20);
Output
false
System.out.println('a' == 'b');
Output
false
System.out.println('a' == 97);
Output
true
Explanation
'a' = 97 (Unicode)
97 == 97
↓
true
System.out.println('a' == 97.0);
Output
true
Even though one operand is a char and the other is a double, Java performs numeric promotion before comparison.
Boolean Example
System.out.println(true == false);
Output
false
System.out.println(false == false);
Output
true
Unlike relational operators, equality operators fully support boolean values.
Equality Operators vs Relational Operators
Many beginners confuse these operators.
| Expression | Result |
|---|---|
true == false |
✅ Valid |
true != false |
✅ Valid |
true > false |
❌ Compile-time error |
true < false |
❌ Compile-time error |
Remember:
- Equality operators work with
boolean. - Relational operators do not.
Rule 2: Equality Operators Can Compare Object References
This is where many Java developers get confused.
For objects, the == operator does not compare the contents.
Instead, it compares whether two references point to the same object in memory.
This is known as reference comparison.
Example
Thread t1 = new Thread();
Thread t2 = new Thread();
Thread t3 = t1;
System.out.println(t1 == t2);
System.out.println(t1 == t3);
Output
false
true
Why?
t1 and t2 point to different objects.
t1 ─────► Thread Object A
t2 ─────► Thread Object B
Since they reference different objects,
t1 == t2
returns
false
Now look at t3.
t1 ──┐
│
└────► Thread Object A
t3 ──┘
Both references point to the same object, so
t1 == t3
returns
true
Rule 3: Object Types Must Be Compatible
The == operator can compare two object references only when the compiler determines that their types are compatible.
Compatibility means the references have one of the following relationships:
- Same type
- Parent → Child
- Child → Parent
Valid Example
Thread thread = new Thread();
Object object = new Object();
System.out.println(thread == object);
Output
false
This compiles successfully because every Thread object is also an Object.
Although the references point to different objects, Java allows the comparison because the types are compatible.
Another example
Object object = new Object();
String developer = new String("Rajesh");
System.out.println(object == developer);
Output
false
Again, String is a subclass of Object, so the comparison is allowed.
Invalid Example
String developer = new String("Rajesh");
Thread thread = new Thread();
System.out.println(developer == thread);
Compile-time error
incomparable types:
java.lang.String and java.lang.Thread
Why?
String and Thread are unrelated classes.
Neither class extends the other.
Since there is no inheritance relationship, Java does not allow the comparison.
Common Beginner Mistakes
Mistake 1: Assuming == Compares Object Contents
String firstName = new String("Rajesh");
String secondName = new String("Rajesh");
System.out.println(firstName == secondName);
Output
false
Many beginners expect true.
However, == compares object references, not the text stored inside the strings.
We'll learn how to compare string contents correctly in Part 2.
Mistake 2: Comparing Unrelated Object Types
String developer = "Rajesh";
Thread thread = new Thread();
System.out.println(developer == thread);
This does not even compile because the reference types are unrelated.
Quick Memory Trick 🧠
For primitive values,
==compares values. For objects,==compares references.
Think of it like this:
- Primitive → Compare the value.
- Object → Compare the memory address.
Key Takeaways
- Java provides two equality operators:
==and!=. - Equality operators always return a boolean value.
- They work with every primitive type, including
boolean. - For primitive values,
==compares actual values. - For object references,
==compares whether both references point to the same object. - Java allows
==only when object reference types are compatible. - Unrelated object types cannot be compared using
==.
Happy Coding!
Top comments (0)