Using new String(String)
to duplicate a string is inefficient and almost always unnecessary.
String
objects can share backing arrays with other String
objects. In those versions, it is possible to leak memory by creating a (small) substring of a (large) string and retaining it. However, from Java 7 onwards, String
backing arrays are not shared.In the absence of any tangible benefit, calling new String(String)
is simply wasteful:
equals(Object)
and hashCode()
can be slower if String objects are copied.