Why ‘==’ and ‘equals’ Differ for Java Strings: 5 Interview Questions Explained
The article walks through five common Java String interview questions, providing code snippets, execution results, and in‑depth analysis of reference equality, object creation in the constant pool, and the impact of the '+' operator, illustrated with memory diagrams to clarify each answer.
st1 and st2 Equality?
public class Demo2_String {
public static void main(String[] args) {
String st1 = "abc";
String st2 = "abc";
System.out.println(st1 == st2);
System.out.println(st1.equals(st2));
}
}First line: true
Second line: true
In Java, the == operator compares primitive values directly, but for reference types it compares the memory addresses of the objects. Since strings are reference types, st1 and st2 point to the same interned constant "abc", both == and equals return true.
The equals method in String is overridden to compare character sequences, returning true only when the sequences are identical.
How Many Objects Are Created?
String st1 = new String("abc");Two objects are created: one in the heap (a copy of the constant) and one in the string constant pool.
The constant pool holds the literal "abc"; the new String call creates a separate heap object that is a copy of that literal, as documented in the JDK API for String(String original).
st1 and st2 Equality with Concatenation
public class Demo2_String {
public static void main(String[] args) {
String st1 = "a" + "b" + "c";
String st2 = "abc";
System.out.println(st1 == st2);
System.out.println(st1.equals(st2));
}
}Both == and equals output true because the compiler optimizes the constant concatenation into the literal "abc", which is interned in the constant pool.
Comparing st2 and st3
public class Demo2_String {
public static void main(String[] args) {
String st1 = "ab";
String st2 = "abc";
String st3 = st1 + "c";
System.out.println(st2 == st3);
System.out.println(st2.equals(st3));
}
} false trueThe == comparison is false because st3 is created at runtime using StringBuilder (or StringBuffer) and therefore resides at a different memory address than the interned literal st2. The equals comparison is true because the character sequences match.
Summary
This set of interview questions tests understanding of Java's string handling, constant pool behavior, reference equality versus content equality, and the effects of the '+' operator, all clarified through code examples and memory diagrams.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Interview Crash Guide
Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
