With the help of StringBuffer and StringBuilder we can also represnt the string but, there is some differnece among them String, StringBuilder, StringBuffer.
This is synchronized and thread safe means methods of this class can’t be called by two threads simulataneously. And its less efficient than String Builder and its mutable and can’t create wiithout new keyword.
Whenever we create an object of this class then internally 16 character capacity is created. When we add more character beyond its capacity then its increase dynamically with the following rules
rule : oldCapacity*2 + 2;
Then its add more character into the String buffer object to its initial capacity and its use this rule only once if intital capacity is 16 and checks the capacity if it is 16,
then it use this rule once and increase the capacity 26 to 34 if it it is not then it creates the capacity as the character of capacity is passed 37 then it make it 37.
But when we pass the string as an areguement when creating sb1 object in the below program then after that getting capacity is 21 becauase it adds initial capacity 16 + with string capcity is given as an arguement in constructor 5.
StringBuilder is not Synchronized so two threads can call the methods simultaneously and its mutable too, and can’t create without new keyword.