Updates
  • Starting New Weekday Batch for Java Full Stack Developer on 24th June 2024 @ 02:00 PM to 04:00 PM
  • Starting New Weekend Batch for Java Full Stack Developer on 06th July 2024 @ 11:00 AM to 02:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 08th July 2024 @ 04:00 PM to 06:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 29th July 2024 @ 10:00 AM to 12:00 PM
Join Course

Java.lang.StringBuffer

• In Java StringBuffer is a built-in class which is available in java.lang package.
• StringBuffer class is a final type of class which means that no other class can inherit the StringBuffer class.
• Objects in the StringBuffer class do not use any special memory allocation like the objects in String class, StringBuffer class objects get memory allocation inside Heap Area.
• We cannot directly create objects of java.lang.StringBuffer class using String literals, like we can do in java.lang.String class.
• StringBuffer class objects are mutable, it means we can update and make changes in the objects of StringBuffer class.

            
class JTC{
	public static void main(String arg[]){

		StringBuffer s1 = new StringBuffer("Hello");
		StringBuffer s2 = s1.append("JTC");

		System.out.println("s1 :- "+s1);
		System.out.println("s2 :- "+s2);
		System.out.println("s1 == s2 :- "+(s1 == s2));
	}
}
    s1 :- HelloJTC
    s2 :- HelloJTC
    s1 == s2 :- true
            

In this example we have a class JTC and inside the JTC class we are creating an object of StringBuffer class along with a String literal “Hello”, and in the next line we are appending “JTC” String literals into “Hello” String literals. As we discussed java.lang.StringBuffer class object is mutable, that is the reason when we are comparing s1 and s2 using == operator we are getting true as the result. So, it proves that StringBuffer class object is mutable unlike String class object.

• StringBuffer class object works on capacity concept. Capacity is a number which represents maximum length of String literals and which can be used to create an object of StringBuffer class.

When we create an object of StringBuffer class using their default constructor, then by default initial capacity will be 16.

StringBuffer s1 = new StringBuffer()

We can create an object of StringBuffer class with custom capacity.

StringBuffer s2 = new StringBuffer(20); [Capacity:- 20]

When we create an object of StringBuffer class object along with a String Literal then the initial capacity will be String Literal length + 16.

StringBuffer s3 = new StringBuffer(“JTC”)

Here we are creating an object of StringBuffer class along with “JTC” String literal so the capacity will be 3 (JTC length) + 16 = 19.

            
class JTC{
	public static void main(String arg[]){
		
		StringBuffer s1 = new StringBuffer();
		System.out.println("s1.capacity :- "+s1.capacity());// 16

		StringBuffer s2 = new StringBuffer(20);
		System.out.println("s2.capacity :- "+s2.capacity());// 20

		StringBuffer s3 = new StringBuffer("JTC");
		System.out.println("s3.capacity :- "+s3.capacity());// 19
	}
}
    s1.capacity :- 16
    s2.capacity :- 20
    s3.capacity :- 19
            

In the above example we can see that we are creating three different objects of StringBuffer class along with three different constructors which are: -

public java.lang.StringBuffer(); public java.lang.StringBuffer(int); public java.lang.StringBuffer(java.lang.String);

And the objects are: -

StringBuffer s1 = new StringBuffer(); StringBuffer s2 = new StringBuffer(20); StringBuffer s3 = new StringBuffer(“JTC”);

And their capacity is: -

For s1 capacity is 16, for s2 capacity is 20 and finally capacity of s3 is 19.

• Now we will discuss how JVM works on capacity when it increases the length of String literals. When length of the String literal increases the current capacity of StringBuffer class object then we do not get any error or exception internally. To get a new capacity we use a formula:

Formula: - New Capacity = (Current Capacity + 1) * 2

            
class JTC{
	public static void main(String arg[]){
		
		StringBuffer s1 = new StringBuffer();
		System.out.println("s1.capacity :- "+s1.capacity());// 16
		System.out.println("s1.length :- "+s1.length()); // 0
		
		s1.append("1234567890111213");
		System.out.println("s1.capacity :- "+s1.capacity());// 16
		System.out.println("s1.length :- "+s1.length()); // 16

		s1.append("14");
		System.out.println("s1.capacity :- "+s1.capacity());// 34
		System.out.println("s1.length :- "+s1.length()); // 18

		s1.append("1516171819202122232425");
		System.out.println("s1.capacity :- "+s1.capacity());// 70
		System.out.println("s1.length :- "+s1.length()); // 40

		s1.append("1234567890123456789012345678901");
		System.out.println("s1.capacity :- "+s1.capacity());// 142
		System.out.println("s1.length :- "+s1.length()); // 71


	}
}
    s1.capacity :- 16
    s1.length :- 0
    s1.capacity :- 16
    s1.length :- 16
    s1.capacity :- 34
    s1.length :- 18
    s1.capacity :- 70
    s1.length :- 40
    s1.capacity :- 142
    s1.length :- 71
            

This example helps us to understand how capacity concept of StringBuffer class works. In the example, first of all we are creating an object of StringBuffer class using default constructor. As per our discussion, their initial capacity is 16 and their length is 0.
In the next section we are appending a String literal into the existing object of StringBuffer class s1 using append method to get the new length as 16. Since the length does not exceed the current capacity, which is 16, the capacity remains the same.

In the next section we are again appending a new String literal which is “14” to change the length to 18, which exceeds the current capacity of 16. Hence as per our previous discussion we apply a formula:

I.e. New Capacity = (Current Capacity + 1) * 2 = (16 + 1) * 2 = 34

In the next section again, we are appending new String literals to change the length to 40 and the capacity with changed length is 70.

In the last movement we can see that after the length of String literal has been appended, the StringBuffer object length shows as 71. The length now exceeds the current capacity which is 70, so we apply the same formula and the new capacity will be 142.

• Some important methods of java.lang.StringBuffer class: -

1. public synchronized int length();

Access Modifier: - public
Return Type: - int
Method Name: - length()
Parameter: - No-Parameter
Functionality: - It returns the length of String literal which we have passed as argument at the time of StringBuffer class object creation. As we can see it is a synchronized method which is based on the Thread safe concept.

2. public synchronized int capacity();

Access Modifier: - public
Return Type: - int
Method Name: - capacity()
Parameter: - No-Parameter
Functionality: - It returns the current capacity of current working StringBuffer class object. As we can see it is a synchronized method which is based on Thread safe concept.

3. public synchronized java.lang.StringBuffer append(java.lang.String);

Access Modifier: - public
Return Type: - java.lang.StringBuffer
Method Name: - append()
Parameter: - java.lang.String
Functionality: - It appends the specified String Literals along with current working StringBuffer class object and returns the same object after append operation.

• Every method present in StringBuffer is synchronized, hence at a time only one thread is allowed to operate on StringBuffer object. This increases the waiting time of the threads and creates performance problems, to overcome this problem we can apply java.lang.StringBuilder.
• Generally, we use java.lang.StringBuffer class object to work with String Literals, like we do in java.lang.String class.
• When we create a module where we frequently need to change the String Literals as per the business logic then we should not work with the String class object. This is because, as we have previously discussed, when we perform concatenation operation along with an existing object of java.lang.String class then always a new object gets created and that might cause memory depletion in the Heap. In this case StringBuffer class object presents a better option as when we use objects of StringBuffer class no unnecessary objects get created.