DEV Community

Rajesh Bhola
Rajesh Bhola

Posted on

Equality Operators (==, !=) in Java — Part 1

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);
Enter fullscreen mode Exit fullscreen mode

Output

true
true
false
Enter fullscreen mode Exit fullscreen mode

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:

  • byte
  • short
  • int
  • long
  • float
  • double
  • char
  • boolean

Numeric Examples

System.out.println(10 == 20);
Enter fullscreen mode Exit fullscreen mode

Output

false
Enter fullscreen mode Exit fullscreen mode

System.out.println('a' == 'b');
Enter fullscreen mode Exit fullscreen mode

Output

false
Enter fullscreen mode Exit fullscreen mode

System.out.println('a' == 97);
Enter fullscreen mode Exit fullscreen mode

Output

true
Enter fullscreen mode Exit fullscreen mode

Explanation

'a' = 97 (Unicode)

97 == 97

↓

true
Enter fullscreen mode Exit fullscreen mode

System.out.println('a' == 97.0);
Enter fullscreen mode Exit fullscreen mode

Output

true
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Output

false
Enter fullscreen mode Exit fullscreen mode
System.out.println(false == false);
Enter fullscreen mode Exit fullscreen mode

Output

true
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Output

false
true
Enter fullscreen mode Exit fullscreen mode

Why?

t1 and t2 point to different objects.

t1 ─────► Thread Object A

t2 ─────► Thread Object B
Enter fullscreen mode Exit fullscreen mode

Since they reference different objects,

t1 == t2
Enter fullscreen mode Exit fullscreen mode

returns

false
Enter fullscreen mode Exit fullscreen mode

Now look at t3.

t1 ──┐
     │
     └────► Thread Object A

t3 ──┘
Enter fullscreen mode Exit fullscreen mode

Both references point to the same object, so

t1 == t3
Enter fullscreen mode Exit fullscreen mode

returns

true
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Output

false
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Output

false
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Compile-time error

incomparable types:
java.lang.String and java.lang.Thread
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Output

false
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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)