EasyExperience level: 0-2 years · 3-5 years1 min read

Why does comparing two Strings with == sometimes pass and sometimes fail in production?

Reviewed by Gurusankar M.

Asked inInfosysTCSCognizantAccentureWipro
#string#==#equals#string pool#interning
Report issue

Short Answer

== compares references; .equals() compares content. String literals share one pooled object (== passes), but Strings built at runtime (new, concatenation, DB/JSON) are distinct objects — so == fails. Always use .equals() for content.

On this page

Concept Made Simple

Coffee Chat Question

“Why does comparing two Strings with == sometimes pass and sometimes fail in production?”

Remember It Faster

Mind Map Answer

== asks *'same object?'*, .equals() asks *'same characters?'*. Literals are interned into the String pool, so two equal literals are the same object — which is why == accidentally works in unit tests and then breaks in prod.

"a" == "a"
true — both pooled
new String("a") == "a"
false — heap object
userInput == "a"
false — runtime String

Learn by Doing

Hands-on Keyboard

java
String a = "INV-100";
String b = new String("INV-100");
System.out.println(a == b);        // reference
System.out.println(a.equals(b));   // content
Output
false
true

Think Beyond the Expected

What If?

Why did the bug only appear once data came from the database?

Literals in code are pooled, so == passed in tests. Strings read from JDBC/JSON are fresh heap objects, so == compares two different references and returns false even when the text is identical.

Real World

A classic enterprise outage: a status check `status == "ACTIVE"` worked in tests (literal vs literal) but silently rejected every record once `status` was hydrated from Oracle, because the DB String was a new object.

Interviewer's Expectation

Keywords they're listening for:

  • reference vs content
  • String pool / interning
  • .equals() for content
  • literals vs new/runtime Strings

Common Mistakes

  • Using == for business-string comparisons
  • Assuming unit-test behavior (pooled literals) matches runtime data
  • Calling status.equals("ACTIVE") and getting an NPE when status is null

Best Practices

  • Compare content with .equals() — or Objects.equals(a, b) for null safety
  • Put the constant first: "ACTIVE".equals(status) to avoid NPEs
  • Reserve == for null checks and enum/primitive comparisons

Follow-up Questions

  1. What does String.intern() do and when would you call it?
  2. How do you null-safely compare — "ACTIVE".equals(status) vs Objects.equals()?
  3. Does == ever work for Integers? (Integer cache -128..127)

Continue Learning with AI

Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.

Plain-language foundations

I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner.

Topic: Strings (Core Java)
Interview question: "Why does comparing two Strings with == sometimes pass and sometimes fail in production?"

Please:
1. Explain the core idea in simple, plain language, using an everyday analogy.
2. Define any technical terms you use.
3. Walk through one small, concrete example.
4. Finish with a single sentence I can easily remember.

Keep the tone friendly and assume I'm new to this topic.
Open inChatGPTGeminiClaude

Was this answer helpful?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.