About Us Contact Us Write for Us Advertise
Home > Java > == vs .equals() in Java: The Difference Explained (Beginner Guide)
Java

== vs .equals() in Java: The Difference Explained (Beginner Guide)

Understand == vs .equals() in Java and fix the most common beginner bug: reference vs content comparison, the String pool gotcha, the rule to memorise, and custom equals().

Shiv Pandey
Shiv Pandey
Oct 14, 2026 | 126 views
== vs .equals() in Java: The Difference Explained (Beginner Guide)

The difference between == and .equals() is behind the single most common bug beginners hit in Java — code that looks correct but behaves mysteriously. It's also a favourite interview question. This short guide makes the distinction crystal clear so you never get caught by it again.

The core difference in one sentence

== checks if two references point to the same object (identity). .equals() checks if two objects have the same content (equality). For objects, you almost always want .equals().

Why this trips everyone up

For primitives (int, char, boolean…), == works exactly as you'd expect — it compares values:

int a = 5, b = 5;
System.out.println(a == b);   // true — compares the values 5 and 5

So beginners naturally assume == compares values everywhere. But for objects (including Strings!), a variable holds a reference — a pointer to an object in memory — and == compares those pointers, not the contents:

String a = new String("hello");
String b = new String("hello");

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

Both strings say "hello", but they're separate objects in memory. == sees two different addresses and says false; .equals() compares the actual characters and says true. This is why comparing Strings with == gives "wrong" results.

The String pool gotcha (why it's extra confusing)

Here's what makes it even trickier. String literals are cached in a "String pool," so identical literals can share one object:

String a = "hello";   // literal — goes in the pool
String b = "hello";   // same literal — reuses the pooled object

System.out.println(a == b);   // true (!) — same pooled object, by luck

This "accidental true" is dangerous: your == comparison seems to work for pooled literals, then fails the moment a string comes from user input, a file, or new String(). That inconsistency is exactly why the rule is absolute: never rely on == for strings.

The rule to memorise

Use == for primitives. Use .equals() for objects (always, for Strings). When in doubt with objects, reach for .equals(). To also guard against a null on the left, use a known-non-null value first: "admin".equals(role), or Objects.equals(a, b).

What about your own classes?

By default, .equals() on your custom class behaves like == (reference check) unless you override it to compare fields. And if you override equals(), you must also override hashCode() so your objects work correctly in HashMap/HashSet. (Full guide: equals() & hashCode().)

I'll be honest — this exact bug cost me hours when I was learning. My login check used == to compare passwords, worked perfectly in my tests with hardcoded strings (pooled literals!), then failed the moment real input came in. The fix was one word: .equals(). The mental model that finally made it stick: an object variable is a label on a box, not the box's contents. == asks "same box?"; .equals() asks "same stuff inside?". Ninety-nine percent of the time you care about the contents. Burn that in, and you'll dodge the most common Java bug there is — and answer the interview question with confidence.

Related

← Core Java course · Java interview questions →

Related Articles

Java Sealed Classes & Pattern Matching Explained (Beginner Guide)
Java

Java Sealed Classes & Pattern Matching Explained (Beginner Guide)

Java Records Explained: Less Boilerplate Data Classes (Beginner Guide)
Java

Java Records Explained: Less Boilerplate Data Classes (Beginner Guide)

What’s New in Java 21? Modern Features Explained (Beginner Guide)
Java

What’s New in Java 21? Modern Features Explained (Beginner Guide)

Spring vs Spring Boot: The Difference Explained (Beginner Guide)
Java

Spring vs Spring Boot: The Difference Explained (Beginner Guide)