Backend Development 9 min read

Five Java String Interview Questions with Answers and Memory Analysis

This article presents five common Java String interview questions, shows the expected outputs, and explains in detail how string literals, the string pool, object creation, and concatenation affect equality comparisons and memory behavior in the JVM.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Five Java String Interview Questions with Answers and Memory Analysis

The article presents five common Java String interview questions, explains the expected output, and provides detailed analysis of string equality, object creation, and memory behavior.

Question 1: Determine whether two literals st1 and st2 are equal using == and equals . 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)); } } Output: true true Explanation: literals share the same object in the string pool, so == returns true; equals compares content and also returns true.

Question 2: How many objects are created by new String("abc") ? String st1 = new String("abc"); Answer: two objects – one in the heap and one in the string pool (the pool object is copied into the heap object).

Question 3: Compare a new String("abc") with a literal "abc". String st1 = new String("abc"); String st2 = "abc"; System.out.println(st1 == st2); System.out.println(st1.equals(st2)); Output: false true Explanation: new creates a distinct heap object, so reference comparison fails; content comparison succeeds.

Question 4: Compare concatenated literals "a" + "b" + "c" with literal "abc" . String st1 = "a" + "b" + "c"; String st2 = "abc"; System.out.println(st1 == st2); System.out.println(st1.equals(st2)); Output: true true Explanation: The compiler folds the concatenation at compile time, producing the same pool object.

Question 5: Compare runtime concatenation "ab" + "c" with literal "abc" . String st1 = "ab"; String st2 = "abc"; String st3 = st1 + "c"; System.out.println(st2 == st3); System.out.println(st2.equals(st3)); Output: false true Explanation: The + operation creates a new StringBuilder at runtime, producing a new heap object; thus reference comparison fails while content comparison succeeds.

Conclusion: Mastering the Java string pool, the difference between == and equals , and compiler optimizations for constant strings is essential for Java interview preparation.

JavaJVMinterviewString()memoryEquality
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.