Core Java APIs· 30 min

Strings, Equality, and Immutability

Master the String pool, immutability, equals, hashCode, StringBuilder, and common interview traps.

Java interview lesson
Question 4 · Not reviewed

Core APIs · objects

Can you explain this clearly in an interview?

Use the strong answer as your reference, explain it aloud in your own words, then choose how soon you want to review it again.

Interview prompt

Strings, Equality, and Immutability

Explain it aloud in your own words

Strong answer

String is immutable, so operations create new values rather than changing an existing instance. String literals may be interned in the string pool, but application code should compare values with equals rather than relying on identity. If equals changes, hashCode must change consistently too.

Key points

  • Use equals for logical equality and == for primitive values or intentional identity checks.
  • Immutable objects are naturally safer to share between threads.
  • A mutable object used as a HashMap key can become unreachable after mutation.
  • Use StringBuilder for repeated concatenation in a loop.

Interviewer looks for

The key trap is explaining why two equal strings can be different objects and why hash-based collections depend on the equals/hashCode contract.

Java example

1String a = new String("java");
2String b = "java";
3System.out.println(a.equals(b)); // true
4System.out.println(a == b);     // usually false

Topic interview questions

Revise this topic like an interviewer

These questions are tied to this lesson. Read each question aloud, then compare your explanation with the strong answer.

5 questions
Q1Intermediate

Why should String values be compared with equals instead of ==?

Strong answer

equals compares the characters in the strings, while == compares whether two references point to the same object. String pooling can make == appear to work in some cases, but it is not a value comparison.

Q2Basic

Why is String immutable in Java?

Strong answer

Immutability makes strings safe to share, supports string pooling, stabilizes their hash codes, and prevents security-sensitive text from changing behind an API. Operations return new strings instead of modifying the original.

Q3Intermediate

What is the equals and hashCode contract?

Strong answer

If two objects are equal according to equals, they must return the same hashCode. Unequal objects may share a hash, but violating the equal-implies-same-hash rule breaks hash-based collections.

Q4Intermediate

When should you use StringBuilder?

Strong answer

Use StringBuilder for repeated concatenation, especially inside loops, when a mutable character buffer avoids creating many intermediate String objects. For a few compile-time or simple concatenations, the + operator is clear and optimized by the compiler.

Q5Intermediate

What does String.intern() do and why should it be used carefully?

Strong answer

intern returns a canonical pooled representation for equal strings, potentially reducing duplicate values but increasing pressure on the shared string pool. It is not a general-purpose memory optimization and should be measured.

How well do you know this?

Your review choice is saved locally and synced to your learner profile when signed in.