Why ‘==’ and ‘equals’ Differ for Java Strings: 5 Interview Questions Explained

The article walks through five common Java String interview questions, providing code snippets, execution results, and in‑depth analysis of reference equality, object creation in the constant pool, and the impact of the '+' operator, illustrated with memory diagrams to clarify each answer.

Java Interview Crash Guide
Java Interview Crash Guide
Java Interview Crash Guide
Why ‘==’ and ‘equals’ Differ for Java Strings: 5 Interview Questions Explained

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

First line: true

Second line: true

In Java, the == operator compares primitive values directly, but for reference types it compares the memory addresses of the objects. Since strings are reference types, st1 and st2 point to the same interned constant "abc", both == and equals return true.

The equals method in String is overridden to compare character sequences, returning true only when the sequences are identical.

Memory diagram for st1 and st2 equality
Memory diagram for st1 and st2 equality

How Many Objects Are Created?

String st1 = new String("abc");

Two objects are created: one in the heap (a copy of the constant) and one in the string constant pool.

The constant pool holds the literal "abc"; the new String call creates a separate heap object that is a copy of that literal, as documented in the JDK API for String(String original).

Memory diagram for new String
Memory diagram for new String

st1 and st2 Equality with 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));
  }
}

Both == and equals output true because the compiler optimizes the constant concatenation into the literal "abc", which is interned in the constant pool.

Comparing st2 and st3

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

The == comparison is false because st3 is created at runtime using StringBuilder (or StringBuffer) and therefore resides at a different memory address than the interned literal st2. The equals comparison is true because the character sequences match.

Memory diagram for st2 vs st3
Memory diagram for st2 vs st3

Summary

This set of interview questions tests understanding of Java's string handling, constant pool behavior, reference equality versus content equality, and the effects of the '+' operator, all clarified through code examples and memory diagrams.

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.

JavaJDKinterviewStringMemoryequality
Java Interview Crash Guide
Written by

Java Interview Crash Guide

Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.

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.