How Many Objects Does a Java String Create? Literal vs new String Explained
This article breaks down the exact number of objects created when using a string literal versus the new String constructor in Java, detailing interactions with the constant pool, stack, and heap, and demonstrates the resulting reference comparisons with concrete code examples.
The article explains how many objects are created in Java when assigning a string literal or using the new String constructor, focusing on the roles of the string constant pool, the stack, and the heap.
1. String a = "abc" – literal assignment
When the literal "abc" is used, the JVM first checks the string constant pool. If the literal does not exist, it is created in the pool. Then a char array containing 'a','b','c' is allocated on the stack, a String object is created on the heap using that array, and finally the reference a is set to point to the pool entry.
常量池中不存在 "abc" 字符串:
(1)在栈中创建 3 个 char 型字符 'a','b','c'
(2)在堆中 new 一个 String 对象,内部保存上述 char 数组
(3)把该 String 对象放入字符串常量池
(4)a 指向常量池中该对象的地址2. Equivalent representation
The same effect can be expressed explicitly with a character array:
char data[] = {'a','b','c'};
String a = new String(data);3. String a = new String("abc") – explicit constructor
This form always creates a new heap object regardless of whether the literal already exists in the constant pool. The steps are:
Define a reference variable a on the stack.
Allocate a new String object on the heap and assign it to a.
The newly created String object internally references the literal "abc" in the constant pool.
4. Reference comparison example
String a = "abc";
String b = "abc";
String c = new String("abc");
String d = new String("abc");
System.out.println(a == b); // true
System.out.println(a == c); // false
System.out.println(c == d); // falseThe output true, false, false shows that literals share the same pool object, while each new String call creates a distinct heap object, so their references are not equal.
5. Conclusion
Using a string literal may create zero or one object (if the literal is already in the pool). Using new String always creates at least one new heap object, and the literal may also reside in the pool, resulting in two objects in total. Therefore, reference equality behaves as demonstrated above.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.
