.equals() vs ==
Master the critical difference between reference equality and value equality in Java to write bug-free object comparisons.
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.
Reference vs Value Equality
== Operator.equals() MethodWhy 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.
The Core Rule
Primitives vs Objects: A Visual
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.
How to Apply the Rule
int, double, char, boolean) or an object (String, Integer, custom class)?==. Primitives store values directly, so == compares the actual values..equals(). This calls the object's comparison method to check content equality.null.equals(x) throws a NullPointerException. Put the known non-null value first: "hello".equals(s).Example 1: String Comparison
new forces a separate heap object.Example 2: Integer Wrapper Trap
Example 3: Custom Objects
Overriding .equals()
Null Safety Pattern
Common Mistakes
new String() or user input is involved.Object.equals(), which uses ==. You must override it to compare fields.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);
new keyword creates two separate objects on the heap. The == operator compares their memory addresses, which are different, so it prints false. Use .equals() to compare content.Question 2 of 5
Which is the correct way to check if two String variables hold the same text?
.equals() compares the character content of two Strings. == only checks if they are the same object. compareTo returns an int for ordering, not a boolean for equality.Question 3 of 5
What happens when you run: String s = null; s.equals("test");?
null throws a NullPointerException at runtime. The safe pattern is "test".equals(s), putting the non-null literal on the left side.Question 4 of 5
Integer x = 127; Integer y = 127; evaluates x == y as true. Why?
== returns true. For values outside this range (like 200), new objects are created and == returns false.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?
.equals() method inherited from Object uses == internally: it checks if both references point to the exact same object. Since two separate objects were created, it returns false even though their fields match. You must override .equals() to compare fields.