Understanding Java String Equality and Memory: Five Interview Questions Explained
This article examines five common Java String interview questions, detailing why each equality check yields true or false by exploring the behavior of the == operator, the equals method, constant pool usage, object creation, and runtime concatenation, all illustrated with code and memory diagrams.
This article presents five typical Java String interview questions, explains the correct answers, and visualizes the underlying memory mechanisms that determine string equality.
Question 1: Compare two literals String st1 = "abc"; and String st2 = "abc"; . Both st1 == st2 and st1.equals(st2) output true because the literals refer to the same object in the string constant pool.
package string;
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));
}
}The == operator checks reference equality; for string literals the JVM reuses the constant‑pool instance, so the references are identical. The equals method compares character sequences, which are also identical.
Question 2: String st1 = new String("abc"); creates two objects – one in the heap and one in the constant pool – because the new keyword forces a distinct heap object that copies the constant‑pool value.
Answer: two objects are created.
Question 3: String st1 = new String("abc"); String st2 = "abc"; . The == comparison is false (different references), while equals is true (same character sequence).
package string;
public class Demo2_String {
public static void main(String[] args) {
String st1 = new String("abc");
String st2 = "abc";
System.out.println(st1 == st2);
System.out.println(st1.equals(st2));
}
}Question 4: Compile‑time concatenation String st1 = "a" + "b" + "c"; is optimized by the compiler to the literal "abc" . Therefore both st1 == st2 and st1.equals(st2) are true .
package string;
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));
}
}Question 5: Runtime concatenation String st3 = st1 + "c"; uses StringBuilder (or StringBuffer ) to create a new String object, so st2 == st3 is false , while st2.equals(st3) is true .
package string;
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));
}
}Each analysis is accompanied by memory‑layout diagrams (omitted here) that illustrate how the JVM handles the constant pool, heap allocation, and the temporary StringBuilder used during runtime concatenation.
In summary, mastering the JDK API documentation, understanding string interning, and visualizing memory operations are essential for correctly answering these interview questions.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
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.