As per my knowledge,
a mutable string can be changed, and an immutable string cannot be changed.
Here I want to change the value of String like this,
String str="Good";
str=str+" Morning";
and other way is,
StringBuffer str= new StringBuffer("Good");
str.append(" Morning");
In both the cases I am trying to alter the value of str
. Can anyone tell me, what is difference in both case and give me clear picture of mutable and immutable objects.
In Java, all strings are immutable. When you are trying to modify a
String
, what you are really doing is creating a new one. However, when you use aStringBuilder
, you are actually modifying the contents, instead of creating a new one.Mutable Objects: When you have a reference to an instance of an object, the contents of that instance can be altered
Immutable Objects: When you have a reference to an instance of an object, the contents of that instance cannot be altered
Let's understand this with an example :
The output from this is:
Now we find that the value displayed by the myString variable has changed. We have defined immutable objects as being unable to change in value, so what is happening? Let's extend the example again to watch the myString variable closer.
The result from executing this is:
What this shows is that variable myString is referencing a new instance of the String class. The contents of the object didn't change; we discarded the instance and changed our reference to a new one with new contents.
Mutable means you will save the same reference to variable and change its contents but immutable you can not change contents but you will declare new reference contains the new and the old value of the variable
Ex Immutable -> String
String x = "value0ne";// adresse one x += "valueTwo"; //an other adresse {adresse two}
adresse on the heap memory change.Mutable -> StringBuffer - StringBuilder
StringBuilder sb = new StringBuilder(); sb.append("valueOne"); // adresse One sb.append("valueTwo"); // adresse One
sb still in the same adresse i hope this comment helps
Case 1:
In the above code you create 3
String
Objects.Note: Strings are always immutable. There is no, such thing as a mutable String.
str
is just a reference which eventually points to "Good Morning". You are actually, not working on1
object. you have3
distinctString
Objects.Case 2:
StringBuffer
contains an array of characters. It is not same as aString
. The above code adds characters to the existing array. Effectively,StringBuffer
is mutable, itsString
representation isn't.Java
String
s are immutable.In your first example, you are changing the reference to the
String
, thus assigning it the value of two otherStrings
combined:str + " Morning"
.On the contrary, a
StringBuilder
orStringBuffer
can be modified through its methods.String in Java is immutable. However what does it mean to be mutable in programming context is the first question. Consider following class,
Now after creating the instance of
Dimension
we can always update it's attributes. Note that if any of the attribute, in other sense state, can be updated for instance of the class then it is said to be mutable. We can always do following,Let's see in different ways we can create a String in Java.
So,
str1
andstr2
are String literals which gets created in String constant poolstr3
,str4
andstr5
are String Objects which are placed in Heap memorystr1 = "Hi!";
creates"Hi!"
in String constant pool and it's totally different reference than"Hey!"
whichstr1
referencing earlier.Here we are creating the String literal or String Object. Both are different, I would suggest you to read following post to understand more about it.
In any String declaration, one thing is common, that it does not modify but it gets created or shifted to other.
It's non changing behaviour, means, the value once assigned can not be updated in any other way. String class internally holds data in character array. Moreover, class is created to be immutable. Take a look at this strategy for defining immutable class.
Shifting the reference does not mean you changed it's value. It would be mutable if you can update the character array which is behind the scene in String class. But in reality that array will be initialized once and throughout the program it remains the same.
As you already guessed, StringBuffer class is mutable itself as you can update it's state directly. Similar to String it also holds value in character array and you can manipulate that array by different methods i.e. append, delete, insert etc. which directly changes the character value array.