Strings are Immutable | StringBuilder | StringBuffer:
In this post, let us discuss about Strings, why Strings are immutable where as StringBuilder and StringBuffer are mutable.
What is Immutability?
When we assign a value to a String variable, that value can never be changed. That is known as immutability.
Strings can be created in 2 ways,
String S1 = “One”;
String S1 = new String(“One”);
What is the difference between these two ways of String creation?
String creation Way 1:
First let us see the type 1, i.e. String S1 = “One”;
For better understanding, let us create 3 strings
In general, Whenever a String is created all the values will be stored in StringPool. Here we have created 3 Strings so all these values – “One”, “Two” and “One” should be stored in StringPool.
Let us see how these above String values are Stored in StringPool.
As we can see the values – “One” and “Two” are stored in StringPool but another value of String S3 = “One” is not stored in StringPool. What actually happened?
When the values are Stored in StringPool when Strings are created in above mentioned way, When two or more strings has the same value only one of that such value will be stored in StringPool and the address of that value in StringPool will be referenced to String Variables.
So from above, since S1 and S3 has same value only one of such value is stored in StringPool and the address Addr1 will be referenced to S1 and S3
So the String variables will point to,
S1 = Addr1
S2 = Addr2
S3 = Addr1
How does “==” Work when Strings are created in above mentioned way?
The “==” works by comparing the address of the String variable rather than comparing the exact value of Strings. So in this case if we try to compare (S1==S3), Since both has same address it will
What if we concatenate the String? Will the value be replaced?
As we discussed, Strings are immutable. That is, once a value has been created that value will never change.
Consider this example,
If we concatenate String S1, what will happen?
S1 = “One” + “Three”;
Will the above statement replace the value One in StringPool? The answer is No.
Now we have concatenated the String, How the values will be at StringPool?
The new concatenated value will be inserted into StringPool and the String Variable S1 will point to new inserted value address – Addr3, Where as the value “One” remains unchanged.
This is the reason Strings are said to be Immutable.
Second way of Creating a String:
Another way of creating a String is using new Keyword, Let us create 3 strings for our understanding.
So we have created 3 Strings – S1,S2 and S3 and assigned the value – “One”, “Two” and “One” using new keyword.
How are these values stored in StringPool?
As you can see the difference, the value “One” is inserted again for the variable S3. Why?
Because the way we created String, We have used new keyword. When we create a String using new keyword, each String is considered as new value and it will be Stored in StringPool even though the same value might be available in StringPool.
So here, The String S1 will have Addr1 pointed to it, S2 will have Addr2 and S3 will have Addr3 – even though S1 and S3 has same value, since we used new keyword to insert the String – it inserted the “One” as new value and new address is given.
How does “==” Work when Strings are created in second String creation way?
Since each value is created newly and inserted in StringPool, The result will always be false for S1==S3 as both S1 and S3 have different addresses.
Why is StringBuilder and StringBuffer are mutable?
StringBuilder internally extends AbstractStringBuilder, it has a value which has an underlying working of Array. Since Arrays are mutable and StringBuilder is an Array wrapped class, it is mutable.
What if we perform Concatenation in StringBuilder?
It will not create a new value, instead it will replace the existing value with newly concatenated value
So what is the difference between StringBuilder and StringBuffer?
The only difference between StringBuilder and StringBuffer is – StringBuilder is not Synchronized (i.e. Not Thread Safe) where as StringBuffer is Synchronized (i.e.Thread Safe)
When considering the performance, StringBuilder is faster than StringBuffer because, StringBuilder is not synchronized.