What is the difference between text and new Stringtext?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
In the Java programming language, understanding the subtle differences between string handling can be crucial for performance and storage optimization. Two common ways to declare strings in Java are using string literals and the new String() constructor. In this article, we’ll explore these two approaches and how they differ.
String Literals
String literals are the most direct way of creating a string in Java. When you write "text" in your code, Java uses a special area in memory called the String Constant Pool. This pool is a dedicated storage area in the heap, and it is optimized for string storage.
When you create a string literal like this:
Java performs the following operations:
- It checks the String Constant Pool to see if a string with the same content already exists.
- If a matching string is found,
str1will reference the existing string. - If no match is found, it creates a new string in the pool, and
str1will reference this new string.
This mechanism enables Java to save memory by avoiding duplicate string instances.
Using new String()
When strings are created using new String("text"), Java will create a new object in the heap, regardless of the contents. This bypasses the String Constant Pool's optimization:
Here’s what happens in this case:
- Java searches the String Constant Pool for the string
"text". - If the string is not already in the pool, it is first added.
- A new
Stringobject is created in the heap with the contents of"text". str2references this new object, not the string in the pool.
The use of new String() is less efficient in terms of memory usage because it creates a new object each time, even if an identical string already exists in the pool.
Key Differences
The differences between string literals and new String() can influence how you manage strings within your Java applications. The table below summarizes these differences:
| Aspect | String Literal ("text") | new String("text") |
| Memory Usage | Uses String Constant Pool | Allocates separate heap memory |
| Memory Efficiency | High due to reusability | Low, as each instance is unique |
| Creation Mechanism | Checks pool before creating | Always creates a new object |
| Performance | Generally faster | Slower due to heap allocation |
Equality (== check) | May result in true for equal content if pointing to the same object | Always false, even for equal content |
Performance Implications
String Creation
Creating strings using literals is faster because Java utilizes the String Constant Pool to reuse existing instances. The process of checking and possibly creating within this pool is inherently faster compared to allocating new memory on the heap.
Memory Usage
String literals are more memory-efficient. Since identical string literals share the same memory location, this reduces the overall footprint of your application. Conversely, new String() can result in larger memory usage since each calls results in a heap allocation.
Equality Considerations
The choice between literals and the new String() method also affects string comparison:
- Reference Equality (
==):- For string literals, the
==operator can returntrueif both variables reference the same object in the String Constant Pool. - For objects created using
new String(),==will always returnfalseunless the references are to the same object.
- Content Equality (
.equals()):- Both methods return
trueif the strings contain the same sequence of characters, since.equals()checks value equivalency rather than reference.
Conclusion
String handling in Java is an interesting topic with depth that can significantly impact performance and memory management. String literals offer optimized memory usage and faster performance by leveraging the String Constant Pool. In contrast, using new String() ensures a fresh string object each time, which can be useful occasionally but is generally less efficient.
Understanding these differences will help write more efficient Java applications, especially as you work with large-scale string operations or memory-sensitive environments. Such knowledge can be pivotal in crafting optimized Java code that balances memory use with performance demands.
Related reading
- What is the difference between using IDisposable vs a destructor in C?
- What is the difference in managed and unmanaged code, memory and size?
- what is the effect of distributed_group_by_no_merge
- What is the efficient way to count set bits at a position or lower?
- What is the difference between the HashMap and Map objects in Java?
- What is the difference between Unidirectional and Bidirectional JPA and Hibernate associations?
- What is the fastest Dijkstra implementation you know in C?
- What is the fastest sort algorithm for 0-65535 integers?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.