Fundamentals 8 min read

Five Common Java String Interview Questions with Detailed Answers

This article walks through five typical Java String interview questions, providing code examples, execution results, and in‑depth explanations of reference equality, the string constant pool, object creation, and compile‑time versus runtime concatenation to help readers master Java string behavior.

Architect's Tech Stack
Architect's Tech Stack
Architect's Tech Stack
Five Common Java String Interview Questions with Detailed Answers

This article presents five frequently asked Java String interview questions, each accompanied by code snippets, output results, and thorough analysis of why the answers are what they are.

Question 1: Determine whether two String variables defined as literals 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)); } } Both statements print true because the literals refer to the same object in the string constant pool and the character sequences are identical.

Question 2: Compare a String created with new String("abc") against a literal. String st1 = new String("abc"); String st2 = "abc"; System.out.println(st1 == st2); System.out.println(st1.equals(st2)); The == comparison yields false (different heap object vs. pool object) while equals yields true because the character sequences match.

Question 3: Compare a literal with a compile‑time concatenated constant. String st1 = "a" + "b" + "c"; String st2 = "abc"; System.out.println(st1 == st2); System.out.println(st1.equals(st2)); Both comparisons return true since the compiler folds the concatenation into a single constant placed in the string pool.

Question 4: Determine equality when a new String object is created and a literal is used. String st1 = new String("abc"); String st2 = "abc"; System.out.println(st1 == st2); System.out.println(st1.equals(st2)); The result is false for == (different memory locations) and true for equals (same character sequence).

Question 5: Compare a literal with a runtime‑concatenated string. String st1 = "ab"; String st2 = "abc"; String st3 = st1 + "c"; System.out.println(st2 == st3); System.out.println(st2.equals(st3)); The == check prints false because st3 is created via StringBuilder at runtime, producing a new object, while equals prints true as the sequences match.

These examples illustrate the crucial differences between reference equality ( == ) and value equality ( equals ), the role of the string constant pool, compile‑time constant folding, and how runtime concatenation creates distinct objects—knowledge essential for Java interview preparation.

JavaJDKinterviewString()memoryEquality
Architect's Tech Stack
Written by

Architect's Tech Stack

Java backend, microservices, distributed systems, containerized programming, and more.

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.