LearnThatStack Ace your next interview
Java · question
Question 3 of 95

Explain the difference between == and equals() method.

beginner
← All Java questions
Re-explain
Visual ↓

For objects, == checks reference equality, meaning both variables point to the same object. equals() checks logical equality, meaning two objects hold the same content, as long as the class overrides it.

For primitives, == compares the values directly. For objects, the default Object.equals() behaves exactly like ==. Classes such as String and Integer override it to compare their values, and your own classes should too when two instances can be logically equal.

Identical string literals are interned, so they share one object in the string pool and s3 == s4 is true. A string built with new String is always a separate object, so compare strings with equals(). A class that overrides equals() must also override hashCode().

String s1 = new String("hello");
String s2 = new String("hello");
String s3 = "hello";
String s4 = "hello";
System.out.println(s1 == s2);        // false (different objects)
System.out.println(s1.equals(s2));   // true (same content)
System.out.println(s3 == s4);        // true (string pool)
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
See it - interactive diagram
== vs equals(): which strings share an object
first new Strings1 = new String("hello");
Stack
s11
Heap
1String
value"hello"
2Pooled String
value"hello"

The literal "hello" goes into the string pool, which keeps one shared copy of each literal. Then new String copies it into a fresh object.

second new Strings2 = new String("hello");
Stack
s11
s2new3
Heap
1String
value"hello"
3Stringnew
value"hello"
2Pooled String
value"hello"

s2 gets its own new object, so s1 == s2 is false. s1.equals(s2) is true, because String's equals() compares the text.

two string literalss3 = "hello"; s4 = "hello";
Stack
s11
s23
s3new2
s4new2
Heap
1String
value"hello"
3String
value"hello"
2Pooled String
value"hello"

Both literals reuse the one pooled hello object, so s3 == s4 is true.

== is true only when both names point at one object. s1 and s2 hold equal text in two objects, while s3 and s4 share one pooled object.

© LearnThatStack - diagrams may not be republished without permission.

Other questions have diagrams like this too. Sign in to ask for one where it's missing - it's free.

Create a free account
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the Java cheatsheet.

← Back to all Java questions
Pro · $10/mo

82 of 95 Java answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime