Consider the following example.
String str = new String();
str = "Hello";
System.out.println(str); //Prints Hello
str = "Help!";
System.out.println(str); //Prints Help!
Now, in Java, String objects are immutable. Then how come the object str
can be assigned value "Help!". Isn't this contradicting the immutability of strings in Java? Can anybody please explain me the exact concept of immutability?
Edit:
Ok. I am now getting it, but just one follow-up question. What about the following code:
String str = "Mississippi";
System.out.println(str); // prints Mississippi
str = str.replace("i", "!");
System.out.println(str); // prints M!ss!ss!pp!
Does this mean that two objects are created again ("Mississippi" and "M!ss!ss!pp!") and the reference str
points to a different object after replace()
method?
Though java tries to ignore it,
str
is nothing more than a pointer. This means that when you first writestr = "Hello";
, you create an object thatstr
points to. When you reassignstr
by writingstr = "Help!";
, a new object is created and the old"Hello"
object gets garbage collected whenever java feels like it.Because String is immutable so changes will not occur if you will not assign the returned value of function to the string.so in your question assign value of swap function returned value to s.
s=swap(s, n1, n2) ;then the value of string s will change.
I was also getting the unchanged value when i was writing the program to get some permutations string(Although it is not giving all the permutations but this is for example to answer your question)
Here is a example.
but i was not getting the permuted values of the string from above code.So I assigned the returned value of the swap function to the string and got changed values of string. after assigning the returned value i got the permuted values of string.
Light_handle I recommend you take a read of Cup Size -- a story about variables and Pass-by-Value Please (Cup Size continued). This will help a lot when reading the posts above.
Have you read them? Yes. Good.
This creates a new "remote control" called "
str
" and sets that to the valuenew String()
(or""
).e.g. in memory this creates:
This then changes the remote control "
str
" but does not modify the original string""
.e.g. in memory this creates:
This then changes the remote control "
str
" but does not modify the original string""
or the object that the remote control currently points to.e.g. in memory this creates:
Use:
If you see here I use the
concat
method to change the original string, that is, "New String" with a string " Added String", but still I got the output as previous, hence it proves that you can not change the reference of object of String class, but if you do this thing by StringBuilder class it will work. It is listed below.Super late to the answer, but wanted to put a concise message from author of the String class in Java
It can be derived from this documentation that anything that changes string, returns different object (which could be new or interned and old). The not so subtle hint about this should come from the function signature. Think about it, 'Why did they make a function on an object return an object instead of status?'.
Also one more source which makes this behaviour explicit (From replace function documentation)
Source: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replace(char,%20char)
Source: JavaDoc of String.
For those wondering how to break String immutability in Java...
Code
Output