The one place where you may think you want new String(String) is to force a distinct copy of the internal character array, as in

small=new String(huge.substring(10,20))

However, this behavior is unfortunately undocumented and implementation dependent.

I have been burned by this when reading large files (some up to 20 MiB) into a String and carving it into lines after the fact. I ended up with all the strings for the lines referencing the char[] consisting of entire file. Unfortunately, that unintentionally kept a reference to the entire array for the few lines I held on to for a longer time than processing the file - I was forced to use new String() to work around it, since processing 20,000 files very quickly consumed huge amounts of RAM.

The only implementation agnostic way to do this is:

small=new String(huge.substring(10,20).toCharArray());

This unfortunately must copy the array twice, once for toCharArray() and once in the String constructor.

There needs to be a documented way to get a new String by copying the chars of an existing one; or the documentation of String(String) needs to be improved to make it more explicit (there is an implication there, but it's rather vague and open to interpretation).

Pitfall of Assuming what the Doc Doesn't State

In response to the comments, which keep coming in, observe what the Apache Harmony implementation of new String() was:

public String(String string) {
    value = string.value;
    offset = string.offset;
    count = string.count;
}

That's right, no copy of the underlying array there. And yet, it still conforms to the (Java 7) String documentation, in that it:

Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string. Unless an explicit copy of original is needed, use of this constructor is unnecessary since Strings are immutable.

The salient piece being "copy of the argument string"; it does not say "copy of the argument string and the underlying character array supporting the string".

Be careful that you program to the documentation and not one implementation.

Answer from Cornelius Dol on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
October 20, 2025 - The class String includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase. Case mapping is based on the Unicode Standard version specified by the Character class. The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings.
🌐
W3Schools
w3schools.com › java › java_strings.asp
Java Strings
A String in Java is actually an object, which means it contains methods that can perform certain operations on strings.
Discussions

Java Strings & Substrings - Code with Mosh Forum
Can anyone pls help me to understand this code, for making the first letter of a word to uppercase. code: Scanner sc=new Scanner(System.in); String A=sc.next(); String B=sc.next(); System.out.print(A.substring(0,1).toUpperCase()+ A.substring(1) + " "+B.substring(0,1).toUpperCase() + ... More on forum.codewithmosh.com
🌐 forum.codewithmosh.com
0
September 9, 2022
What is the purpose of the expression "new String(...)" in Java? - Stack Overflow
While looking at online code samples, I have sometimes come across an assignment of a String constant to a String object via the use of the new operator. For example: String s; ... s = new String(" More on stackoverflow.com
🌐 stackoverflow.com
android - What is "String..." in java? - Stack Overflow
Possible Duplicate: varargs and the '…' argument Java, 3 dots in parameters I saw this definition in my android java file. It looks just like String[]. Are they different? Than... More on stackoverflow.com
🌐 stackoverflow.com
JEP 430: String Templates (Preview)
Personally I find it most interesting and innovative that this is apparently going to include validation, basically allowing for safely writing SQL statements in a String Template even with untrusted arguments. For example: ResultSet rs = DB."SELECT * FROM Person p WHERE p.last_name = \{name}"; Edit: I was made aware that Scala has a similar feature: https://docs.scala-lang.org/overviews/core/string-interpolation.html#advanced-usage More on reddit.com
🌐 r/java
86
89
August 20, 2022
🌐
JSON
json.org
JSON
JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others.
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › lang › String.html
String (Java SE 11 & JDK 11 )
January 20, 2026 - The class String includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase. Case mapping is based on the Unicode Standard version specified by the Character class. The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings.
🌐
Medium
medium.com › geekculture › a-deep-dive-into-java-string-e5f67ccbdba8
A deep dive into Java String. Do you really know Java strings? | by Tech & Math | Geek Culture | Medium
March 22, 2022 - To be able to understand all of those outcomes, we need to first dive into the working mechanism of the String class. In Java’s implementation of String class, we can see it contains final char[] value. It contains the final keyword, so we know the String class value can only be assigned once, making the value attribute immutable.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.string
String Class (Java.Lang) | Microsoft Learn
The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.
Find elsewhere
🌐
Minecraft Wiki
minecraft.wiki › w › String
String – Minecraft Wiki
January 24, 2026 - 6.1 Java Edition · 6.2 Bedrock Edition · 6.3 Legacy Console Edition · 6.4 New Nintendo 3DS Edition · 7 Issues · 8 Gallery · 8.1 Renders · 8.2 Screenshots · 8.3 In other media · 9 References · 10 External links · 11 Navigation · [edit | edit source] [edit | edit source] Tripwire can be broken instantly with any tool, dropping string.
🌐
Code with Mosh
forum.codewithmosh.com › t › java-strings-substrings › 15036
Java Strings & Substrings - Code with Mosh Forum
September 9, 2022 - Can anyone pls help me to understand this code, for making the first letter of a word to uppercase. code: Scanner sc=new Scanner(System.in); String A=sc.next(); String B=sc.next(); System.out.print(A.substring(0,1).toUpperCase()+ A.substring(1) + " "+B.substring(0,1).toUpperCase() + ......
🌐
CodeGym
codegym.cc › java blog › strings in java › java strings
Java Strings
March 4, 2025 - In programming, strings are very commonly used. String in java is an object that represents a sequence of characters backed by a char array. String class is immutable in Java and implements Comparable, Serializable, and CharSequence interfaces.
Top answer
1 of 9
84

The one place where you may think you want new String(String) is to force a distinct copy of the internal character array, as in

small=new String(huge.substring(10,20))

However, this behavior is unfortunately undocumented and implementation dependent.

I have been burned by this when reading large files (some up to 20 MiB) into a String and carving it into lines after the fact. I ended up with all the strings for the lines referencing the char[] consisting of entire file. Unfortunately, that unintentionally kept a reference to the entire array for the few lines I held on to for a longer time than processing the file - I was forced to use new String() to work around it, since processing 20,000 files very quickly consumed huge amounts of RAM.

The only implementation agnostic way to do this is:

small=new String(huge.substring(10,20).toCharArray());

This unfortunately must copy the array twice, once for toCharArray() and once in the String constructor.

There needs to be a documented way to get a new String by copying the chars of an existing one; or the documentation of String(String) needs to be improved to make it more explicit (there is an implication there, but it's rather vague and open to interpretation).

Pitfall of Assuming what the Doc Doesn't State

In response to the comments, which keep coming in, observe what the Apache Harmony implementation of new String() was:

public String(String string) {
    value = string.value;
    offset = string.offset;
    count = string.count;
}

That's right, no copy of the underlying array there. And yet, it still conforms to the (Java 7) String documentation, in that it:

Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string. Unless an explicit copy of original is needed, use of this constructor is unnecessary since Strings are immutable.

The salient piece being "copy of the argument string"; it does not say "copy of the argument string and the underlying character array supporting the string".

Be careful that you program to the documentation and not one implementation.

2 of 9
10

The only time I have found this useful is in declaring lock variables:

private final String lock = new String("Database lock");

....

synchronized(lock)
{
    // do something
}

In this case, debugging tools like Eclipse will show the string when listing what locks a thread currently holds or is waiting for. You have to use "new String", i.e. allocate a new String object, because otherwise a shared string literal could possibly be locked in some other unrelated code.

🌐
Tutorialspoint
tutorialspoint.com › home › java/lang › java string class
Java String Class
September 1, 2008 - In Java programming language, strings are treated as objects.
🌐
Medium
medium.com › geekculture › all-about-string-in-java-51ba9e46181a
All About String in Java. Java Strings are objects that are… | by sajith dilshan | Geek Culture | Medium
February 8, 2023 - All About String in Java Java Strings are objects that are backed by a char array and are immutable. The class java.lang.String is used to create a string object. There are two ways to create a …
🌐
TheServerSide
theserverside.com › definition › Java-string
What Is a Java String?
Java strings are an important element of the Java programming language. Learn how they can be used and manipulated.
🌐
Kotlin
kotlinlang.org › docs › basic-syntax.html
Basic syntax overview | Kotlin Documentation
{ if (obj is String) { // `obj` is automatically cast to `String` in this branch return obj.length } // `obj` is still of type `Any` outside of the type-checked branch return null } //sampleEnd fun main() { fun printLength(obj: Any) { println("Getting the length of '$obj'.
🌐
GeeksforGeeks
geeksforgeeks.org › java › string-arrays-in-java
String Arrays in Java - GeeksforGeeks
October 2, 2025 - A String Array in Java is an array that stores string values.
🌐
Android Developers
developer.android.com › api reference › string
String | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
Computer Science Circles
cscircles.cemc.uwaterloo.ca › java_visualize
Java Visualizer
At the top of your program, write import java.util.Stack; — note, import java.util.*; won't work.
🌐
Medium
medium.com › @AlexanderObregon › a-beginners-guide-to-java-strings-3485cba0b517
Java Strings Guide For Beginners | Medium
February 23, 2024 - A string in Java is fundamentally an object that represents a sequence of characters. Unlike in languages like C, where strings are typically handled as arrays of characters, Java treats strings as objects of the String class, encapsulated within ...
🌐
DigitalOcean
digitalocean.com › community › tutorials › what-is-java-string-pool
What is Java String Pool? | DigitalOcean
August 3, 2022 - If there is already a string literal “Cat” in the pool, then only one string “str” will be created in the pool. If there is no string literal “Cat” in the pool, then it will be first created in the pool and then in the heap space, so a total of 2 string objects will be created. Read: Java String Interview Questions