Mutable String With String Builder and String Buffer - ayushmathur94/DirectQuesAns_Prep GitHub Wiki

Mutable means changing over time or that can be changed. In a mutable string, we can change the value of string and JVM doesn’t create a new object i.e. In a mutable string, we can change the value of the string in the same object.
To create a mutable string in java, Java has two classes StringBuffer and StringBuilder.

Mutability :

String is an example of an immutable type. A String object always represents the same string. StringBuilder is an example of mutable type. It has methods to delete parts of the string, insert or replace characters, etc.

Since String is immutable, once created, a String object always has the same value. To add something to the end of a String, you have to create a new String object:
s

String s = "a";
s = s.concat("b"); // s+="b" and s=s+"b" also mean the same thing

By contrast, StringBuilder objects are mutable. This class has methods that change the value of the object, rather than just returning new values:
s2

StringBuilder sb = new StringBuilder("a");
sb.append("b"); 

StringBuilder has other methods as well, for deleting parts of the string, inserting in the middle, or changing individual characters.

So what? In both cases, you end up with s and sb referring to the string of characters "ab". The difference between mutability and immutability doesn’t matter much when there’s only one reference to the object. But there are big differences in how they behave when there are other references to the object. For example, when another variable t points to the same String object as s, and another variable tb points to the same StringBuilder as sb, then the differences between the immutable and mutable objects become more evident:

String t = s;
t = t + "c";

StringBuilder tb = sb;
tb.append("c");

t
This shows that changing t had no effect on s, but changing tb affected sb too.

Since we have the immutable String class already, why do we even need the mutable StringBuilder in programming?

A common use for it is to concatenate a large number of strings together. Consider this code:

String s = "";
for (int i = 0; i < n; ++i) {
    s = s + n;
}

Using immutable strings, this makes a lot of temporary copies in the course of building up the final string. It actually costs O(n2) time just to do all that copying, even though we only concatenated n elements.

StringBuilder is designed to minimize this copying. It uses a simple but clever internal data structure to avoid doing any copying at all until the very end, when you ask for the final String with a toString() call:

StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; ++i) {
  sb.append(String.valueOf(i));
}
String s = sb.toString();

Getting good performance is one reason why we use mutable objects. Another is convenient sharing: two parts of your program can communicate more conveniently by sharing a common mutable data structure.

public class MutableString
{
    public static void main (String[] args)
    {
        StringBuffer str1 = new StringBuffer("JavaGoal");
        StringBuilder str2 = new StringBuilder("Learning");
        
        System.out.println("Value of str1 before change :" + str1);
        System.out.println("Value of str2 before change :" + str2);
        
        str1.append(".com");
        str2.append(" website");
        
        System.out.println("Value of str1 after change :" + str1);
        System.out.println("Value of str2 after change :" + str2);
    }
}

Output: Value of str1 before change :JavaGoal
Value of str2 before change :Learning
Value of str1 after change :JavaGoal.com
Value of str2 after change :Learning website

In the above example, we are creating two mutable strings one is from StringBuffer and another from StringBuilder. Here we are changing the value of string after creation. We can see we don't need to assign the value to assign in the same object, unlike immutable strings.

⚠️ **GitHub.com Fallback** ⚠️