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.StringBuilder

• In Java, StringBuilder is a built-in class available in java.lang package like StringBuffer class.
• Almost all the functionalities of StringBuilder class are same as StringBuffer class.
• StringBuilder class is final type of class as StringBuffer.
• We cannot directly create objects of java.lang.StringBuilder class using String literals, like we do in java.lang.StringBuffer class.
• StringBuilder class objects are mutable.

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

		StringBuilder s1 = new StringBuilder("Hello");
		StringBuilder 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 StringBuilder class along with a String literal which is “Hello”, and in the next line we are appending “JTC” String literals into “Hello” String literals. As we have discussed, java.lang.StringBuilder class objects are mutable; that is the reason when we are comparing s1 and s2 using == operator we are getting true as the result.

• StringBuilder class objects like StringBuffer class objects are based on capacity concept.

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

StringBuilder s1 = new StringBuilder();

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

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

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

StringBuilder s3 = new StringBuilder (“JTC”);

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

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

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

		StringBuilder s3 = new StringBuilder ("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 StringBuilder class along with three different constructors which are: -

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

And the objects are: -

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

And their capacity is: -

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

• In StringBuilder we use same formula that we use in StringBuffer to get the new capacity.

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

            
class JTC{
	public static void main(String arg[]){
		
		StringBuilder s1 = new StringBuilder ();
		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 StringBuilder class works. In the example, first of all we are creating an object of StringBuilder class using default constructor and 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 StringBuilder class s1 using append method to get their new length as16; since new length does not exceed the current capacity of 16, so capacity remains the same.

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

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

In the next section again, we are appending a String literal that has length of 40 and capacity of 70.

In the last movement we can see that after appending the length of String literal, we get the StringBuilder object length as 71. Since the new length exceeds the current capacity of 70, so we can apply the same formula to get the new capacity of 142.

As we know that every method of java.lang.StringBuffer class is synchronized, which means that more than one thread can access the class methods concurrently. Due to this, the application response time can increase. StringBuilder class methods are non-synchronized whereas StringBuffer class methods are synchronized; barring this feature, methods of both the classes serve same purpose and hence can mostly be used interchangeably. We can use the StringBuffer class objects when we face dead lock situations in our module, otherwise it is always better to use StringBuilder class objects.