String Literal vs new String() in Java
Understand the difference between string literals and new String() in Java, including memory behavior, interning, and when to use each approach.

Java's new String() constructor is often misunderstood. Many developers assume it is the standard way to create a string, but in practice a string literal is usually simpler and more efficient. A literal such as "hello" is interned and reused by the JVM, while new String("hello") deliberately creates a separate String object. This article explains the difference, how interning works, when the constructor is genuinely useful, and common equality and performance pitfalls.
The Difference Between a Literal and new String()
A string literal is a sequence of characters enclosed in double quotes, such as "hello". In modern Java, literals are stored in the string constant pool, an area of the heap. The JVM reuses identical literals, so two assignments of the same literal point to the same object.
String a = "hello";
String b = "hello";
System.out.println(a == b); // trueThe new String("hello") constructor creates a new String object at runtime, even if its argument is a literal. This object is not automatically added to the pool and occupies separate memory.
String c = new String("hello");
String d = new String("hello");
System.out.println(c == d); // false
System.out.println(c.equals(d)); // trueHere, c and d are distinct objects with identical content. The == operator compares references, not values, so it returns false. The equals method compares the underlying character sequence, so it returns true. This distinction is the root of many subtle bugs when developers rely on reference equality.
How String Interning Affects Literals
The JVM maintains a string constant pool that contains interned strings. When you use a literal, the JVM checks the pool first. If an equal string already exists, the literal resolves to that existing object. This process is called interning.
The String.intern() method returns the canonical form of a string and adds it to the pool if it is not already present. Calling it on a new String() object makes the reference comparable with the literal.
String e = new String("hello").intern();
String f = "hello";
System.out.println(e == f); // trueInterning has a cost: the pool must be maintained, and adding entries can increase memory pressure if the pool grows large. In most applications, relying on literal interning is sufficient, and explicit calls to intern() are rarely needed.
Memory and Allocation Costs of new String()
Unlike a literal, new String() asks for a new object each time it is executed. The JIT compiler can sometimes eliminate allocations it proves are not observable, but application code should not rely on that. In a loop that creates thousands of strings, allocating a new object each iteration can lead to excessive garbage collection and higher memory consumption.
for (int i = 0; i < 1000; i++) {
String s = new String("data"); // 1000 separate requests for a new object
}Using a literal inside the loop reuses the same interned object and avoids a per-iteration String allocation. The difference is especially pronounced in long-running applications where object churn affects throughput and latency.
The String class is immutable, so once created, its value cannot change. This immutability enables safe sharing of interned literals across threads. A new String() object offers no additional safety; it simply wastes memory when the same value already exists in the pool.
When new String() Is Justified
There are a few scenarios where new String() is appropriate. One is when you need to create a String from a character array and want to avoid retaining the array reference. The String(char[]) constructor copies the array elements, so the original array can be modified or garbage collected without affecting the string.
char[] chars = {'j', 'a', 'v', 'a'};
String s = new String(chars);
chars[0] = 'x'; // s remains "java"Another case is decoding bytes with a specific charset. The String(byte[], Charset) constructor is the standard way to convert raw bytes into a string, and it is not redundant because the input is not a string literal.
// rawBytes is a byte[] obtained from an external source
String decoded = new String(rawBytes, StandardCharsets.UTF_8);One case that is rarely justified is new String(existingString). Because strings are immutable, copying an existing string creates a redundant object.
In older Java versions (before Java 7), substring() shared the underlying character array with the original string, which could cause memory leaks. Developers used new String(substring) to trim the backing array. Since Java 7, substring() copies the characters, so this workaround is no longer necessary.
Common Misconceptions and Pitfalls
A frequent mistake is using new String() to "reset" a string or to create a mutable copy. Strings are immutable, so no constructor changes that. Another misconception is that new String() is required for equality checks. The equals() method works correctly on literals and constructed strings alike.
String g = "hello";
String h = new String("hello");
System.out.println(g.equals(h)); // trueSome developers also believe that new String() is faster than literals because it bypasses pool lookup. In reality, the pool lookup is cheap, and allocating a new object is usually more expensive. The JVM may optimize away redundant allocation in some cases, but relying on that is risky.
A related pitfall is using == to compare strings that were constructed at runtime. Even if two strings have the same content, reference equality will fail unless both are interned. Always use equals() for value comparison.
Performance Considerations in Hot Paths
In performance-sensitive code, minimizing object allocation is crucial. Creating new String() objects inside loops, request handlers, or data-processing pipelines increases GC pressure. The JVM's escape analysis can sometimes eliminate allocations, but it is not guaranteed for all code paths.
Consider a method that builds a string from parts:
String result = "";
for (String part : parts) {
result += part; // creates a new String on each iteration
}This pattern creates a new String object on each iteration. In older javac versions, it compiled to a fresh StringBuilder per iteration; newer JDKs use a different runtime strategy, but the allocation cost remains. Using StringBuilder directly avoids the intermediate strings and is the recommended approach for dynamic concatenation.
StringBuilder sb = new StringBuilder();
for (String part : parts) {
sb.append(part);
}
String result = sb.toString();Similarly, using new String() in a hot path is almost always a mistake. If you need a copy of a string, consider whether the copy is truly necessary. The immutability of strings means that sharing references is safe and often preferable.
Alternatives: String.valueOf(), StringBuilder, and String.join()
For most string-creation needs, Java provides better tools than new String(). String.valueOf() converts primitives and objects to their string representation. For example, String.valueOf(42) returns the string "42"; it builds the string from the numeric value rather than copying a string literal.
int number = 42;
String s = String.valueOf(number); // "42"When building strings from multiple parts, StringBuilder is the standard choice. It maintains a mutable buffer and avoids the overhead of creating intermediate strings. The toString() method at the end produces a single immutable String object.
StringBuilder sb = new StringBuilder();
sb.append("User: ");
sb.append(userId);
sb.append(", role: ");
sb.append(role);
String logLine = sb.toString();In Java 8 and later, String.join() offers a concise way to join a collection of strings with a delimiter. It internally uses StringJoiner, which is similar to StringBuilder but specialized for delimited lists.
List<String> names = Arrays.asList("Alice", "Bob", "Carol");
String joined = String.join(", ", names); // "Alice, Bob, Carol"These alternatives make the intended conversion explicit and avoid creating a needless duplicate of an existing string. The constructor remains mainly useful for conversions where you need to copy or decode data, such as from character arrays or byte arrays with a specific charset.
Try matching browser tools
All toolsSmall utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Java guides
All Java guidesJava Final Keyword: Usage and Behavior
How Java's final keyword works for variables, methods, and classes, with practical examples and decision guidance for using it.
Java Final Variable: Syntax, Behavior, and Use Cases
Understand what a final variable in Java means, how initialization works, and when to use it for constants, immutability, and clearer code.
Java Duration vs Period: When to Use Each
java duration vs period: Compare Java Duration and Period to handle time-based and date-based calculations correctly. Learn syntax, conversion, and pitfalls.
Instance vs Static Variables in Java: Key Differences
Understand the difference between instance and static variables in Java, including memory, lifecycle, and when to use each.
More in Java
All Java guidesThe guides filed next to this one in Java, in library order.