How should I copy Strings in Java?

Java

Java Problem Overview


    String s = "hello";
    String backup_of_s = s;
    s = "bye";

At this point, the backup variable still contains the original value "hello" (this is because of String's immutability right?).

But is it really safe to copy Strings with this method (which is of course not safe to copy regular mutable objects), or is better to write this? :

    String s = "hello";
    String backup_of_s = new String(s);
    s = "bye";

In other words, what's the difference (if any) between these two snippets?


EDIT - the reason why the first snippet is safe:

Let me just explain things with a little more detail, based on the good answers already provided (which were essentially focused on the question of difference of performance between the 2 snippets):

Strings are immutable in Java, which means that a String object cannot be modified after its construction. Hence,

String s = "hello"; creates a new String instance and assigns its address to s (s being a reference to the instance/object)

String backup_of_s = s; creates a new variable backup_of_s and initializes it so that it references the object currently referenced by s.

Note: String immutability guarantees that this object will not be modified: our backup is safe

Note 2: Java garbage collection mechanism guarantees that this object will not be destroyed as long as it is referenced by at least one variable (backup_of_s in this case)

Finally, s = "bye"; creates another String instance (because of immutability, it's the only way), and modifies the s variable so that it now references the new object.

Java Solutions


Solution 1 - Java

Since strings are immutable, both versions are safe. The latter, however, is less efficient (it creates an extra object and in some cases copies the character data).

With this in mind, the first version should be preferred.

Solution 2 - Java

Strings are immutable objects so you can copy them just coping the reference to them, because the object referenced can't change ...

So you can copy as in your first example without any problem :

String s = "hello";
String backup_of_s = s;
s = "bye";

Solution 3 - Java

Your second version is less efficient because it creates an extra string object when there is simply no need to do so.

Immutability means that your first version behaves the way you expect and is thus the approach to be preferred.

Solution 4 - Java

Second case is also inefficient in terms of String pool, you have to explicitly call intern() on return reference to make it intern.

Solution 5 - Java

String str1="this is a string";
String str2=str1.clone();

How about copy like this? I think to get a new copy is better, so that the data of str1 won't be affected when str2 is reference and modified in futher action.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSébastienView Question on Stackoverflow
Solution 1 - JavaNPEView Answer on Stackoverflow
Solution 2 - JavaalerootView Answer on Stackoverflow
Solution 3 - JavaDavid HeffernanView Answer on Stackoverflow
Solution 4 - JavaAmit YadavView Answer on Stackoverflow
Solution 5 - JavaCqiao13View Answer on Stackoverflow