Interviewer asked me why is “hello” == “hello” True but new String(“hello”) == “hello” False in…

Interviewer asked me why is “hello” == “hello” True but new String(“hello”) == “hello” False in Java?

The mind-bending truth about Java’s String pool and why it treats identical strings like different objects.

Interviewer asked me why is “hello” == “hello” True but new String(“hello”) == “hello” False in Java?

Hello friends, I have shared a lot of questions on System Design and DSA in past and today, I am going to share another great interview questions from my recent Java interview.

I remember sitting in that interview room, confident about my Java knowledge.

I had mastered OOP, understood inheritance, and could explain polymorphism in my sleep.

“This should be easy,” I thought.

And then the interviewer dropped this bomb:

String a = "hello";
String b = "hello";
System.out.println(a == b); // true
String x = new String("hello");
String y = "hello";
System.out.println(x == y); // false

My brain just… stopped working.

How the hell is “hello” == “hello” true but new String(“hello”) == “hello” false?

Aren’t they both just… strings?

Spoiler alert: Java doesn’t care about your logic. It cares about memory optimization and will mess with your head to save a few bytes.

🧠 The Mind-Bending Truth

Here’s what’s actually happening behind the scenes:

1. String Literals Go to the Pool

When you write “hello”, Java doesn’t create a new object every time. Instead, it checks the String Pool (a special area in heap memory) first.

If “hello” already exists in the pool, Java says “Why waste memory? Let’s reuse it!”

2. new String() Forces Object Creation

When you use new String(“hello”), you’re explicitly telling Java: “I don’t care about efficiency, create a NEW object!”

Java obeys and creates a brand new String object in regular heap memory, separate from the pool.

3. == Compares References, Not Content

The == operator compares memory addresses, not actual content.

• String literals point to the same memory location in the pool → true

• new String() creates a different memory location → false

💡 The Fix That Saves Your Sanity

// Always use .equals() for content comparison
String x = new String("hello");
String y = "hello";
System.out.println(x.equals(y)); // true ✅
// Or use Objects.equals() to handle nulls safely
System.out.println(Objects.equals(x, y)); // true ✅

🎯 Pro Tips:

  • Never use == for String comparison in production code
  • Always use .equals() for content comparison
  • Use Objects.equals() when nulls might be involved
  • Remember: == compares references, .equals() compares values

Once I learned this, that interview question became my favorite way to mess with other developers. 😈

Have you been bitten by this Java quirk before? Share your horror stories in the comments below!

💬 What other Java interview questions have caught you off guard? Let me know and I’ll create more posts breaking them down!

By the way, I regularly use Grokking the Java Interview book by javinpaul for my Java interview preparation. It’s a great collection of questions, divided in two volumes and I highly recommend it.

You can download the free PDF here and you can also use code friends20 to get 20% discount now.


Interviewer asked me why is “hello” == “hello” True but new String(“hello”) == “hello” False in… was originally published in Javarevisited on Medium, where people are continuing the conversation by highlighting and responding to this story.

This post first appeared on Read More