Java String Declaration: Performance and Memory Differences Between new String() and String Literals

Dec 03, 2025 · Programming · 12 views · 7.8

Keywords: Java String | String Pool | Performance Optimization

Abstract: This article explores two Java string declaration methods: using the new String() constructor and direct string literals. It analyzes the string pool mechanism, memory allocation principles, and performance impacts, explaining why string literal declaration is recommended. Code examples and memory model diagrams are included to help developers optimize string handling and avoid unnecessary object creation.

Introduction

In Java programming, strings are among the most frequently used data types, yet the choice of declaration method is often overlooked by developers. Based on core knowledge from the Q&A data, this article systematically analyzes the differences between String str = new String("SOME") and String str = "SOME", with a focus on performance and memory management.

Comparison of String Declaration Mechanisms

String declaration in Java involves two distinct memory allocation mechanisms. When using new String("SOME"), a new String object is created on the heap memory, regardless of whether an identical string exists in the string pool. For example:

String s1 = new String("hello");

Upon execution, this code first checks if "hello" exists in the string constant pool; if not, it adds it to the pool. Then, the new keyword forces the creation of a new object on the heap with the value "hello". Thus, each invocation results in a new object reference.

In contrast, direct string literal declaration:

String s2 = "hello";

This method directly references an existing object in the string pool. If "hello" is not present in the pool, it is added and its reference returned; if it already exists, the existing reference is returned, avoiding duplicate object creation.

Analysis of Performance and Memory Impact

Performance differences primarily arise from object creation and memory usage. Using new String() leads to unnecessary heap memory allocation, increasing garbage collection overhead. For instance, frequent use in loops can significantly degrade performance. String literal declaration leverages pooling mechanisms to reduce memory footprint and enhance runtime efficiency.

The following code example visually demonstrates reference comparison differences:

String s1 = new String("hello");
String s2 = "hello";
String s3 = "hello";
System.err.println(s1 == s2); // Outputs false
System.err.println(s2 == s3); // Outputs true

Here, s1 == s2 returns false because s1 points to a new object on the heap, while s2 points to an object in the string pool; s2 == s3 returns true as both reference the same pooled object.

Best Practices and Optimization Recommendations

Based on the analysis, string literal declaration should be prioritized in most scenarios to improve performance and reduce memory overhead. However, new String() can be used when explicit creation of independent objects is needed (e.g., in security-sensitive contexts). Developers should understand how the string pool works to avoid misusing the constructor in performance-critical code.

Supplementing with insights from other answers, constants are optimized into the same instance at compile time, further reinforcing the advantages of literal declaration. Therefore, during code reviews and optimizations, it is advisable to check and replace unnecessary new String() calls.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.