Backend Development 8 min read

Five Java String Interview Questions with Detailed Answers and Memory Analysis

This article presents five common Java String interview questions, explains the output of each code snippet, analyzes the behavior of the == operator and equals method, and illustrates memory allocation in the string constant pool and heap with diagrams and detailed explanations.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Five Java String Interview Questions with Detailed Answers and Memory Analysis

This article examines five typical Java String interview questions, providing the original code examples, the expected output, and a thorough analysis of why each result occurs.

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));
  }
}

Output: true true

The == operator compares object references; because both literals refer to the same entry in the string constant pool, the references are identical. The equals method is overridden in String to compare character sequences, so it also returns true .

How many objects are created?

String st1 = new String("abc");

Two objects are created: one String object in the heap and one literal "abc" in the constant pool. The heap object is a copy of the constant‑pool object.

st1 and st2 equality with new String

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));
  }
}

Output: false and true. st1 points to the heap object, while st2 points to the constant‑pool literal, so the references differ; the character sequences are identical, so equals returns true .

st1 and st2 equality with compile‑time 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));
  }
}

Output: true and true. The compiler folds the constant concatenation into the literal "abc", so both references point to the same constant‑pool object.

st2 and st3 equality with runtime concatenation

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));
  }
}

Output: false and true. The + operation creates a new StringBuilder , appends the parts, and converts the result to a new String object, so st3 resides at a different address from the constant‑pool st2 . The character sequences match, so equals returns true .

Overall, mastering these nuances of the Java String class, the constant pool, and reference vs. value comparison is essential for answering interview questions correctly.

Javamemory managementJDKinterviewString()Equality
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

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.