DevNotes
Support
Core Java · Chapter 05

== vs equals()

Reference comparison vs logical equality, with String pool, wrapper caching, enum and null-check cases.

◷ 6 min read§ 17 sections? 8 interview questions{ } 24 code examples

#Introduction

One of the most frequently asked Java interview questions is:

What is the difference between == and equals()?

Although both are used for comparison, they serve completely different purposes. Using the wrong one can introduce subtle bugs into your application.

#What is ==?

== is a Java operator. It compares:

  • Primitive values
  • Object references (memory addresses)

The behaviour depends on the type being compared.

#== with Primitive Types

For primitive data types, == compares actual values.

Java
int a = 10;
int b = 10;
System.out.println(a == b);
Output
true

Another example:

Java
double x = 10.5;
double y = 20.5;
System.out.println(x == y);
Output
false

For primitives, == compares the stored values.

#== with Objects

For objects, == compares references, not object contents.

Java
Employee e1 = new Employee();
Employee e2 = new Employee();
System.out.println(e1 == e2);
Output
false
e1
Object A
e2
Object B
Different addresses → different references → == returns false

Same Reference Example

Java
Employee e1 = new Employee();
Employee e2 = e1;
System.out.println(e1 == e2);
Output
true
e1e2
Object A
Both references point to the same object

#What is equals()?

equals() is a method defined in the Object class. Its purpose is to compare logical equality.

Default implementation:

Java
public boolean equals(Object obj) {
    return this == obj;
}

By default, equals() behaves exactly like ==. Only after overriding it does logical comparison become possible.

Example Without Overriding

Java
Employee e1 = new Employee();
Employee e2 = new Employee();
System.out.println(e1.equals(e2));
Output
false

Reason: the default implementation compares references.

Example With Overriding

Java
import java.util.Objects;

class Employee {
    int id;

    Employee(int id) {
        this.id = id;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (!(obj instanceof Employee))
            return false;
        Employee other = (Employee) obj;
        return id == other.id;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}

Usage:

Java
Employee e1 = new Employee(101);
Employee e2 = new Employee(101);
System.out.println(e1.equals(e2));
Output
true

Although they are different objects, they represent the same employee.

#== vs equals()

==equals()
OperatorMethod
Compares references (objects)Compares logical equality
Compares values (primitives)Can be overridden
FasterSlightly slower due to method call
Cannot be customizedFully customizable

#Memory Example

Java
Employee e1 = new Employee(101);
Employee e2 = new Employee(101);
e1
Object Aid = 101
e2
Object Bid = 101
ExpressionOutputWhy
e1 == e2falseReferences differ
e1.equals(e2)trueIDs are equal

#String Comparison

This is where most beginners get confused.

Java
String s1 = "Java";
String s2 = "Java";
System.out.println(s1 == s2);
Output
true

Why? Because both strings are stored in the String Constant Pool.

s1s2
String Pool"Java"
Both variables point to the same pooled object

Using new

Java
String s1 = new String("Java");
String s2 = new String("Java");
s1
Heap · Object A"Java"
s2
Heap · Object B"Java"
Two separate heap objects
Java
System.out.println(s1 == s2);
Output
false

Because two separate objects were created. But:

Java
System.out.println(s1.equals(s2));
Output
true

Because String overrides equals() to compare characters.

String Pool Example

Java
String s1 = "Hello";
String s2 = "Hello";
String s3 = new String("Hello");
System.out.println(s1 == s2);
System.out.println(s1 == s3);
System.out.println(s1.equals(s3));
Output
true
false
true

#Wrapper Class Example

Java
Integer a = 100;
Integer b = 100;
System.out.println(a == b);
Output
true

Why? Because Java caches wrapper objects from -128 to 127.

Now:

Java
Integer a = 200;
Integer b = 200;
System.out.println(a == b);
Output
false

Different objects are created. But:

Java
System.out.println(a.equals(b));
Output
true

#Null Comparison

Wrong:

Java
String s = null;
s.equals("Java");
Output
NullPointerException

Correct:

Java
Objects.equals(s, "Java");
Output
false

#Real Project Examples

Entity Comparison

Java
employee1.equals(employee2)

Compare business identity.

Enum Comparison

Java
status == Status.ACTIVE

Enums are singletons. Using == is recommended.

Null Checks

Java
if (obj == null)

Always use ==. Never use obj.equals(null).

Comparing Strings

Correct:

Java
username.equals(input)
// or
Objects.equals(username, input)

Wrong:

Java
username == input

#Interview Questions

Q1What does == compare?
  • Primitive values
  • Object references
Q2What does equals() compare?

Logical equality.

Q3Can equals() be overridden?

Yes. It is designed for customization.

Q4Can == be overridden?

No. It is an operator.

Q5Why does "Java" == "Java" return true?

Because both references point to the same object in the String Constant Pool.

Q6Why does new String("Java") == new String("Java") return false?

Because two separate heap objects are created.

Q7Should enums be compared using == or equals()?

Use ==. It is faster and safe because each enum constant exists only once.

Q8Why shouldn't we compare strings using ==?

Because == compares references instead of character content.

#Common Mistakes

  • Comparing strings with ==.
  • Using equals() without checking for null.
  • Assuming == compares object contents.
  • Forgetting that wrapper classes cache values between -128 and 127.

#Best Practices

  • Use == for primitive values.
  • Use == for enum comparisons.
  • Use == for null checks.
  • Use equals() for comparing object contents.
  • Use Objects.equals() when either value may be null.

#Quick Revision

Primitive
==
Compare Values
Objects
==
Compare References
Objects
equals()
Compare Contents

#Interview Cheat Sheet

ScenarioUse
Primitive comparison==
String comparisonequals()
Enum comparison==
Null check==
Business object comparisonequals()
Nullable object comparisonObjects.equals()

#Chapter Summary

After completing this chapter, you should be able to:

  • Explain the difference between == and equals().
  • Understand reference comparison versus logical equality.
  • Explain why String behaves differently from most classes.
  • Understand wrapper object caching and its effect on ==.
  • Know when to use ==, equals(), and Objects.equals() in real-world Java applications.
  • Confidently answer one of the most common Java interview questions.