Possible Duplicate:
What is the purpose of the expression “new String(…)” in Java?
If immutable classes objects copies would be equal to the originals then why does the String class in Java have a copy constructor? Is it a mistake or there is a reason behind this implementation?
In the Java docs it is specified that:
/**
* Initializes a newly created {@code 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 {@code original} is needed, use of this constructor is
* unnecessary since Strings are immutable.
*
* @param original
* A {@code String}
*/
public String(String original) {
....
....}
The main reason to copy a string is to “trim the baggage”, that is to trim the underlying char array to only what is necessary.
The underlying char array can mainly be too big because when you create a string by calling
substring, the char array can be shared between the new string instance and the source string instance; an offset points to the first character and the length is included.The expression I use, “trim the baggage”, is taken from the source code of String copying constructor :
This is something many developers forget and is important because a small string may prevent the garbaging of a bigger char array. See this related question where I already pointed this : Java not garbage collecting memory. Many developers consider than the decision of Java designers to use this old optimization trick that was familiar to C coders did, in fact, more harm than good. Many of us know it because we were bitten by it and did have to look into Sun’s source code to understand what happened…
As Marko points out (see comments below), in OpenJDK, starting from java 7 Update 6,
substringdoesn’t share the char array anymore and theString(String)constructor is, thus, useless. But it’s still fast (even faster in fact) and as this change hadn’t been propagated to all VM (and probably not all your customers) I’d recommend to keep this best-practice to usenew String(substring)when the old behavior was justifying it.