Understanding Java String Concatenation, Constant Pool, and Bytecode Behavior
This article explains how Java handles String literals and concatenation, detailing the roles of the String constant pool, the effects of the '+' operator invoking StringBuilder.append, the placement of resulting strings in the heap versus the pool, and demonstrates these concepts through bytecode analysis and code examples.
In Java interview questions, String handling is frequently examined, covering topics such as the String constant pool, the use of new String() , the difference between == and equals() , and the native method String.intern() .
String constant pool
new String()
Difference between == and equals()
Native method String.intern()
A specific interview question is presented:
String str3 = "what";
String str4 = str3 + " a nice day";
// At runtime, '+' is equivalent to new, so the heap contains a "what a nice day" object,
// and the constant pool contains "what" and " a nice day" but not "what a nice day".
// Does the constant pool contain "what a nice day"?The author initially thought the concatenated string would not be in the constant pool and later confirmed that the '+' operation actually invokes StringBuilder.append (not StringBuffer ) and that toString() is called to create a new String object.
Code Verification
public static void main(String[] args) {
// #1
String str1 = "what";
// #2
String str2 = str1 + " a nice day";
// #3
System.out.println("what a nice day".equals(str2));
// #4
System.out.println("what a nice day" == str2);
}Questions to answer:
Where is str1 stored?
Where is str2 stored?
Is the result of equals() true or false?
Is the result of == true or false?
Where does the literal "what a nice day" reside?
Analysis Based on JDK 1.8 Bytecode
Running javap -verbose -p Main.class reveals the constant pool entries:
#2 = String // what
#6 = String // a nice day
#9 = String // what a nice dayFrom this we can answer:
str1 is stored in the constant pool.
The literal "what a nice day" (the separate constant, not str2 ) is also stored in the constant pool.
The bytecode shows that the '+' operation translates to calls to StringBuilder.append and finally StringBuilder.toString() , which creates a new String object on the heap.
str2 resides on the heap.
equals() returns true .
== returns false .
Thus the expression str1 + " a nice day" is equivalent to:
String str2 = new StringBuilder().append(str1).append(" a nice day").toString();The toString() implementation of StringBuilder creates a new String instance, confirming that str2 is not placed in the constant pool.
Summary
By inspecting the generated bytecode, we can see the exact mechanisms Java uses for String concatenation, revealing that although the operation appears simple, it involves heap allocation, constant‑pool entries, and the StringBuilder API. Understanding these details helps answer interview questions more confidently than memorising superficial facts.
Try It Yourself
/**
* What is the output of the following program?
*/
public static void main(String[] args) {
String str1 = "what";
String str2 = str1 + " a nice day";
System.out.println("what a nice day".equals(str2));
System.out.println("what a nice day" == str2);
} /**
* What is the output of the following program?
*/
public static void main(String[] args) {
String str1 = "what a nice day";
String str2 = new String("what a nice day");
System.out.println(str1.equals(str2));
System.out.println(str1 == str2);
} /**
* What is the output of the following program?
*/
public static void main(String[] args) {
String str1 = "what";
String str2 = str1.concat(" a nice day");
System.out.println("what a nice day".equals(str2));
System.out.println("what a nice day" == str2);
System.out.println("what a nice day" == str2.intern());
}These examples let you experiment with equals() versus == , the effect of new String() , and the impact of intern() on the constant pool.
Reference: Original article
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.