248
 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.

5 Answers 5

175

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Immutability has nothing to do with it. It's simply how object references work. I could provide an equivalent StringBuilder example.
@BalusC, I can't see how new String() could create anything in the String pool of the JVM. Only string litterals and those commited to the pool via intern() are in the pool.
@GriffeyDog: I am reading the question less literally. What I am saying is that it is safe to give out references to a string object without fear that someone might modify the string.
@GriffeyDog I find your comment very misleading: immutability is what makes the first snippet safe, why would you say it has nothing to do with "it" ?
@Sebastien All it's doing is reassigning the reference variable s to refer to a different object (the String "bye"). That doesn't affect what the reference variable backup_of_s is referring to (the String "hello"). Like I said, I could provide an equivalent example with StringBuilders, which are not immutable. My comment is mainly in regard to the OPs statement: At this point, the backup variable still contains the original value "hello" (this is because of String's immutability right?).
|
28

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"; 

Comments

10

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.

Comments

0

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

Comments

-15
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.

2 Comments

String is immutable. cloning Strings doesn't make much sense.
it does in certain context where you want to compare objects and references...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.