DEV Community

Rajesh Bhola
Rajesh Bhola

Posted on

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

In Part 1, we learned that:

  • == and != work with all primitive data types.
  • For primitive values, == compares actual values.
  • For objects, == compares references (memory addresses).
  • Object reference types must be compatible for == to compile.

In this part, we'll explore some of the most frequently asked Java interview topics, including null, == vs .equals(), the String Pool, Integer Cache, and best practices.


Rule 4: Comparing with null

The null literal represents a reference that points to no object.

Example 1: Reference Points to an Object

String developer = new String("Rajesh");

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

Output

false
Enter fullscreen mode Exit fullscreen mode

Since developer refers to an actual object, it is not null.


Example 2: Reference Is null

String developer = null;

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

Output

true
Enter fullscreen mode Exit fullscreen mode

Here, the reference doesn't point to any object.


Example 3: Comparing null with null

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

Output

true
Enter fullscreen mode Exit fullscreen mode

Both operands represent the same null reference.

Important Rule

  • If a reference points to an object, reference == null returns false.
  • If the reference itself is null, reference == null returns true.

Rule 5: == vs .equals() (Most Asked Java Interview Question)

This is one of the most common interview questions.

Many beginners assume that == compares object contents.

It doesn't.

Example

String firstName = new String("Rajesh");
String secondName = new String("Rajesh");

System.out.println(firstName == secondName);
System.out.println(firstName.equals(secondName));
Enter fullscreen mode Exit fullscreen mode

Output

false
true
Enter fullscreen mode Exit fullscreen mode

Why?

Two different objects are created.

firstName ───► "Rajesh"

secondName ─► "Rajesh"
Enter fullscreen mode Exit fullscreen mode

The text is the same, but the objects are different.

Therefore,

firstName == secondName
Enter fullscreen mode Exit fullscreen mode

returns

false
Enter fullscreen mode Exit fullscreen mode

However,

firstName.equals(secondName)
Enter fullscreen mode Exit fullscreen mode

compares the contents and returns

true
Enter fullscreen mode Exit fullscreen mode

The Difference Between == and .equals()

== Operator .equals() Method
Operator Method
Works with primitives and objects Works only with objects
Compares values for primitives Not applicable for primitives
Compares references for objects Usually compares object contents
Cannot be overridden Can be overridden
Faster Depends on implementation

String Pool Interview Question

Another very popular interview question.

String first = "Rajesh";
String second = "Rajesh";
String third = new String("Rajesh");

System.out.println(first == second);
System.out.println(first == third);

System.out.println(first.equals(third));
Enter fullscreen mode Exit fullscreen mode

Output

true
false
true
Enter fullscreen mode Exit fullscreen mode

Why?

String literals are stored in the String Pool.

first ──┐
         │
second ──┘──► "Rajesh" (String Pool)

third ─────► New String Object
Enter fullscreen mode Exit fullscreen mode
  • first and second reference the same pooled object.
  • third references a new object created using new.

Integer Cache Interview Question

Wrapper classes also have caching behavior.

Integer a = 100;
Integer b = 100;

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

Output

true
Enter fullscreen mode Exit fullscreen mode

Now look at this.

Integer x = 200;
Integer y = 200;

System.out.println(x == y);
System.out.println(x.equals(y));
Enter fullscreen mode Exit fullscreen mode

Output

false
true
Enter fullscreen mode Exit fullscreen mode

Why?

Java caches wrapper objects for values between -128 and 127.

Values outside this range create separate objects.


Incompatible Types

Consider the following code.

String developer = new String("Rajesh");

StringBuffer buffer = new StringBuffer("Rajesh");

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

Compile-time error

incomparable types:
String and StringBuffer
Enter fullscreen mode Exit fullscreen mode

Why?

String and StringBuffer are unrelated classes.

Java does not allow reference comparison between unrelated types.


But .equals() Works

String developer = new String("Rajesh");

StringBuffer buffer = new StringBuffer("Rajesh");

System.out.println(developer.equals(buffer));
Enter fullscreen mode Exit fullscreen mode

Output

false
Enter fullscreen mode Exit fullscreen mode

No compile-time error occurs.

The equals() method simply returns false.


Why Does String.equals() Behave Differently?

The String class overrides the equals() method to compare characters.

String first = new String("Rajesh");
String second = new String("Rajesh");

System.out.println(first.equals(second));
Enter fullscreen mode Exit fullscreen mode

Output

true
Enter fullscreen mode Exit fullscreen mode

Now look at StringBuffer.

StringBuffer first = new StringBuffer("Rajesh");
StringBuffer second = new StringBuffer("Rajesh");

System.out.println(first.equals(second));
Enter fullscreen mode Exit fullscreen mode

Output

false
Enter fullscreen mode Exit fullscreen mode

StringBuffer does not override equals().

Therefore, it inherits the default implementation from Object, which performs reference comparison.


Classes That Override equals()

Many commonly used Java classes override equals() for content comparison.

Examples include:

  • String
  • Wrapper classes (Integer, Long, Double, etc.)
  • Most collection classes (ArrayList, HashSet, HashMap, etc.)

Safely Comparing with null

This code is safe.

String developer = null;

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

Output

true
Enter fullscreen mode Exit fullscreen mode

However,

String developer = null;

System.out.println(developer.equals("Rajesh"));
Enter fullscreen mode Exit fullscreen mode

throws

NullPointerException
Enter fullscreen mode Exit fullscreen mode

A safer approach is

"Rajesh".equals(developer)
Enter fullscreen mode Exit fullscreen mode

or

Objects.equals(developer, "Rajesh")
Enter fullscreen mode Exit fullscreen mode

The Objects.equals() method safely handles null values.


Arrays and Equality

Another popular interview question.

int[] numbers1 = {1, 2, 3};
int[] numbers2 = {1, 2, 3};

System.out.println(numbers1 == numbers2);
System.out.println(numbers1.equals(numbers2));
Enter fullscreen mode Exit fullscreen mode

Output

false
false
Enter fullscreen mode Exit fullscreen mode

Neither compares array contents.

The correct way is

Arrays.equals(numbers1, numbers2);
Enter fullscreen mode Exit fullscreen mode

Output

true
Enter fullscreen mode Exit fullscreen mode

Relationship Between == and .equals()

Given Conclusion
r1 == r2 is true r1.equals(r2) is always true
r1 == r2 is false .equals() may be true or false
r1.equals(r2) is true == may be true or false
r1.equals(r2) is false == is always false

Common Beginner Mistakes

Using == to Compare Strings

Incorrect

String first = new String("Rajesh");
String second = new String("Rajesh");

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

Use

first.equals(second)
Enter fullscreen mode Exit fullscreen mode

Calling .equals() on a null Reference

Incorrect

developer.equals("Rajesh");
Enter fullscreen mode Exit fullscreen mode

Correct

"Rajesh".equals(developer)
Enter fullscreen mode Exit fullscreen mode

or

Objects.equals(developer, "Rajesh")
Enter fullscreen mode Exit fullscreen mode

Assuming Wrapper Objects Behave Like Primitives

Remember that wrapper objects are objects.

== compares references.

.equals() compares values.


Best Practices

  • Use == for comparing primitive values.
  • Use == only when checking whether two object references point to the same object.
  • Use .equals() when comparing object contents.
  • Use Objects.equals() when a reference may be null.
  • Use Arrays.equals() to compare array contents.
  • Avoid using == for comparing strings unless you intentionally want to compare references.

Quick Memory Trick 🧠

== checks whether it's the same object. .equals() checks whether the contents are the same.

Think of two books:

  • Same physical book → ==
  • Different books with identical text → .equals()

Key Takeaways

  • Equality operators work with all primitive data types, including boolean.
  • For primitives, == compares values.
  • For objects, == compares references.
  • .equals() is intended for content comparison and can be overridden.
  • String overrides equals(), but StringBuffer does not.
  • String literals are stored in the String Pool.
  • Wrapper classes cache values from -128 to 127.
  • Use Objects.equals() for null-safe comparisons.
  • Use Arrays.equals() to compare array contents.

Happy Coding!

Top comments (0)