Why does comparing two Strings with == sometimes pass and sometimes fail in production?
⚡ 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.
☕Coffee Chat Question
Concept Made Simple
“Why does comparing two Strings with == sometimes pass and sometimes fail in production?”
🧠Mind Map Answer
Remember It Faster
== 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.
⌨️Hands-on Keyboard
Learn by Doing
String a = "INV-100";
String b = new String("INV-100");
System.out.println(a == b); // reference
System.out.println(a.equals(b)); // contentfalse true
🔥What If?
Think Beyond the Expected
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:
⚠️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
- 1What does String.intern() do and when would you call it?
- 2How do you null-safely compare — "ACTIVE".equals(status) vs Objects.equals()?
- 3Does == ever work for Integers? (Integer cache -128..127)
🧩Related Technologies
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.
Was this answer helpful?
⭐ Featured Products
Support our platform by exploring our recommended products.
As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.