5 Java String Interview Questions Explained with Memory Diagrams

This article walks through five common Java String interview questions, showing code examples, output results, and detailed memory‑layout analyses to explain why each comparison behaves as it does, helping readers master string handling in Java.

Programmer DD
Programmer DD
Programmer DD
5 Java String Interview Questions Explained with Memory Diagrams

This article presents five Java String interview questions that the author has encountered personally, providing code snippets, expected outputs, and thorough analyses of why the results occur.

1. Equality of two literals

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: first line: true second line: true

Analysis: In Java, the == operator compares object references for reference types. String literals are stored in the string constant pool, so both st1 and st2 refer to the same pooled object, making st1 == st2 true. The equals method is overridden in String to compare character sequences, also returning true.

2. Using new String("abc")

String st1 = new String("abc");

Answer: two objects are created – one in the heap and one in the string constant pool. The heap object is a copy of the pooled literal.

3. Literal vs. 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));
    }
}

Result: false and true. The new creates a distinct heap object, so reference comparison fails, while equals succeeds because the character sequences match.

4. Concatenation of literals

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

Result: true and true. The compiler folds the literal concatenation into a single constant, so both references point to the same pooled object.

5. Concatenation with a variable

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

Result: false and true. The expression st1 + "c" is evaluated at runtime using StringBuilder, producing a new String object on the heap, so reference comparison fails while content comparison succeeds.

In summary, mastering the differences between literal pooling, the new operator, and runtime concatenation is essential for correctly answering Java String interview questions.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaJDKStringMemory
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

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.