โ† Java
#3 most askedVery commonEasyCore Java & OOP

String immutability, the string pool, and == vs equals

New to Core Java & OOP? Learn it from scratch first
Inheritance vs Composition โ†’
๐Ÿข Asked in real interviews
No reports yet. Were you asked this? Be the first to say where.
I was asked this

Why are Java strings immutable? What's the string pool, and why does == sometimes "work" for strings?

๐Ÿ”ฎ Predict the output

String a = "hello";
String b = "hello";
String c = new String("hello");
System.out.println(a == b);
System.out.println(a == c);
System.out.println(a.equals(c));

๐ŸŽค Answer it out loud, then check yourself

Say your answer as if the interviewer is listening (30โ€“90 seconds). Then reveal what a strong answer covers and tick what you said.

    Answer out loud first, then compare.
    Want to rehearse it?
    Answer this to the AI interviewer and get a verdict โ†’