01 / 21
==
.equals()
vs

.equals() vs ==

Master the critical difference between reference equality and value equality in Java to write bug-free object comparisons.

Java Object-Oriented Programming

What Are Objects in Memory?

In Java, when you write String s = "hello";, two things are created: a reference variable (stored on the stack) and the actual object (stored on the heap). The variable s does not hold the string itself: it holds the memory address pointing to where the object lives.

STACK s t HEAP "hello" @0x7A "hello" @0xB3

Reference vs Value Equality

== Operator
Compares memory addresses. Asks: "Do these two variables point to the exact same object in the heap?"
.equals() Method
Compares content/value. Asks: "Do these two objects contain the same data, even if they live at different addresses?"

Why Does This Matter?

Using == when you meant .equals() is one of the most common bugs in Java. It compiles without errors, runs without exceptions, but produces wrong results silently.

String a = new String("hello"); String b = new String("hello"); System.out.println(a == b); // false (different objects!) System.out.println(a.equals(b)); // true (same content)

The Core Rule

For Objects: use .equals()
For Primitives: use ==
Primitives (use ==)
int, double, char, boolean float, long, short, byte Stored directly on stack
Objects (use .equals())
String, Integer, ArrayList Custom classes, arrays Reference on stack, data on heap

Primitives vs Objects: A Visual

Primitives x 42 y 42 x == y true Values compared directly Objects s t "hello" "hello" s == t false Different addresses! s.equals(t) true

Key Distinction: Same Object vs Same Value

== .equals()
What it checks Same address in memory Same content/value
Works on primitives? ✓ Yes (correct way) ✕ No (primitives have no methods)
Works on objects? Compiles, but checks identity ✓ Yes (correct way)
Can return different results? Yes: two objects with identical data can be == false but .equals() true

The String Pool Twist

Java optimizes memory with a String Pool. When you use string literals (without new), Java reuses the same object. This means == can sometimes return true for Strings, which misleads beginners.

// Literals: same pool object String a = "hello"; String b = "hello"; System.out.println(a == b); // true (same pool object!) // new keyword: different objects String c = new String("hello"); System.out.println(a == c); // false (different objects) System.out.println(a.equals(c)); // true (same content)

How to Apply the Rule

1
Identify the type. Is the variable a primitive (int, double, char, boolean) or an object (String, Integer, custom class)?
2
If primitive: use ==. Primitives store values directly, so == compares the actual values.
3
If object: use .equals(). This calls the object's comparison method to check content equality.
4
Guard against null. Calling null.equals(x) throws a NullPointerException. Put the known non-null value first: "hello".equals(s).

Example 1: String Comparison

String name1 = "Alice"; String name2 = "Alice"; String name3 = new String("Alice"); System.out.println(name1 == name2); // ? System.out.println(name1 == name3); // ? System.out.println(name1.equals(name3)); // ?
name1 == name2
true: Both literals share the same String Pool object.
name1 == name3
false: new forces a separate heap object.
name1.equals(name3)
true: Content is the same: "Alice".

Example 2: Integer Wrapper Trap

Integer x = 127; Integer y = 127; System.out.println(x == y); // true (cached!) Integer a = 200; Integer b = 200; System.out.println(a == b); // false (not cached!) System.out.println(a.equals(b)); // true (same value)

Example 3: Custom Objects

class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } Point p1 = new Point(3, 4); Point p2 = new Point(3, 4); System.out.println(p1 == p2); // false System.out.println(p1.equals(p2)); // false (!)

Overriding .equals()

@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Point other = (Point) obj; return this.x == other.x && this.y == other.y; }

Null Safety Pattern

String input = null; // DANGER: NullPointerException! input.equals("admin"); // SAFE: put the literal first "admin".equals(input); // returns false, no crash // OR: use Objects.equals() Objects.equals(input, "admin"); // returns false, null-safe

Common Mistakes

✕ Using == for Strings
Works for literals due to the String Pool, but breaks when new String() or user input is involved.
✕ Forgetting to Override
Custom classes inherit Object.equals(), which uses ==. You must override it to compare fields.
✕ Calling .equals() on null
If the left side is null, you get a NullPointerException. Always guard or reverse the order.

Summary Comparison

Scenario Use Result
Two int values == Compares values directly
Two String objects .equals() Compares character content
Two Integer wrappers .equals() Compares numeric value
Custom class instances .equals() (override it!) Compares fields you define
Checking if same object == True only for same reference
Null check == null Safe: no method call

Question 1 of 5

What does the following code print?
String a = new String("hi"); String b = new String("hi"); System.out.println(a == b);

Question 2 of 5

Which is the correct way to check if two String variables hold the same text?

Question 3 of 5

What happens when you run: String s = null; s.equals("test");?

Question 4 of 5

Integer x = 127; Integer y = 127; evaluates x == y as true. Why?

Question 5 of 5

You create a Student class without overriding .equals(). Two Student objects with identical fields are compared using .equals(). What is the result?