StringBuffer Vs StringBuilder
StringBuffer Vs StringBuilder
There are mainly three ways to create StringBuffer objects in Java. Let’s
understand them one by one.
First way:
The first way for creating a StringBuffer object is that first allocate the memory
for the StringBuffer object by using the new operator and then store the string
into it.
Once the 16th character is completed, the next capacity will be 34 if we add
17th character.
Let’s take an example program based on it. Look at the program source code
to understand better.
Program source code 1:
package stringBufferPrograms;
public class CapacityTest {
public static void main(String[ ] args)
{
// Create a StringBuffer object.
StringBuffer sb = new StringBuffer();
int length = sb.length();
int capacity = sb.capacity();
// Now add the 17th character in the existing string buffer object.
sb.append("q");
Output:
Before adding any character:
Length = 0
Default initial capacity = 16
As you can observe, up to the 16th character, still capacity is 16 only. This is
because a new StringBuffer object has been created with a new capacity
when we added the 17th character.
When 34 characters are completed and added 35th character then a new
string buffer object with a bigger size is again created.
New capacity = ( 34 + 1 ) * 2 = 70.
Thus, StringBuffer has a growable nature. If the StringBuffer object reaches its
maximum capacity, a bigger StringBuffer object is created internally.
Second way:
We can create an object of StringBuffer class by using the new operator and
pass the string to the object. The general syntax to create StringBuffer object
with the specified string is given below:
StringBuffer sb = new StringBuffer(String s);
For example:
StringBuffer sb=new StringBuffer("Tech");
Capacity = sb.length() + 16 = 4 + 16 = 20
Here, we are passing string “Tech” in the StringBuffer object and “sb” is
pointing to that object i.e. the address of the StringBuffer object is stored in
reference variable sb.
Let’s take an example program based on it.
Program source code 2:
package stringBufferPrograms;
public class CapacityTest2 {
public static void main(String[ ] args)
{
StringBuffer sb = new StringBuffer("Tech");
int length = sb.length();
int capacity = sb.length() + 16;
System.out.println(length); // 4
System.out.println(capacity); // 20
}
}
Output:
4 20
Third way:
We can also create an empty StringBuffer object by declaring a size as an
argument for storing characters. The general syntax is as follows:
StringBuffer sb = new StringBuffer(int initialCapacity);
For example:
StringBuffer sb = new StringBuffer(30);
Here, an empty StringBuffer object has been created with a capacity for
storing 30 characters. But it is also possible to store more than 30 characters
in this object because StringBuffer is mutable and can expand dynamically in
the memory.
Suppose that we have a string “Hello” in string constant pool and it is fixed
and not changing frequently, we will need string class. Till now there is no
problem.
Now assume that we want to add content “Java” with string “Hello”. In this
case, compulsory a new object will be created with this change because a
string object is immutable. We are not allowed to change in the existing
object.
Now suppose the content is changing 10 times internally. This means that 10
times new objects will be created if the string will be used. In this situation,
performance will be gone to be down. Memory problems will also be created
in such a situation.
To overcome this kind of requirement, we should need or use a StringBuffer. If
we will use StringBuffer object and 10 times content is changing then still one
object will be created.
All the required changes will be performed in the existing object only. But if it
is string, for every small change, a new object will be created.
Due to which memory will waste and performance will be down. This is the
biggest advantage of StringBuffer technique in Java programming.
Key points:
1. In the case of String, for every small change, a new object will be created.
2. If StringBuffer is used, for all required changes, will perform in the existing
object only. No new object will be created in the memory by JVM.
sb.setLength(20);
System.out.println("After setLength, New StringBuilder: " +sb);
}
}
Output:
Length: 29
Capacity: 45
New capacity: 92
After setLength, New StringBuilder: Every people love hi
Output:
String form of StringBuilder: Java Technology
Substring: Java Tech
There are two main advantages of using StringBuilder class in Java over
StringBuffer. They are as:
1. StringBuilder is more efficient if it is accessed by just a single thread,
because no synchronization is required in this case.
2. It provides faster performance because thread is not synchronized.
3. Multiple threads can access the same string builder object at a time.
1. String is suitable to use when you do not want to modify the content in the
string object. String is an immutable class. Once created, its content cannot
be changed.
2. StringBuffer can be used when we want to perform any changes in the
content and need synchronization. It can be used when you are working in
multithreading environment.
3. StringBuilder can be used when you want to create a string within a single
thread. It will improve the execution time and performance.
StringBuilder is suitable to use when no thread-safety is needed.