Why does comparing two Strings with == sometimes pass and sometimes fail in production?
Reviewed by Gurusankar M.
β‘ 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.