• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Interview Questions
  • Java
  • Java Programs
  • Selenium
  • Selenium Programs
  • Manual Testing
  • Test Cases
  • Difference
  • Tools
  • SQL
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Tutorial » StringBuilder Class In Java with Examples

StringBuilder Class In Java with Examples

Last Updated on: May 8, 2022 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • What is StringBuilder in java?
  • StringBuilder Class Constructors
  • Methods of StringBuilder Class
  • What is the use of String builder?
  • Why do we use String builder in Java?
  • Is String builder efficient?:
  • Which is better: StringBuilder or String?:

StringBuilder Java  In Java: StringBuilder in Java is a class used to create mutable character strings. Strings class provides an immutable succession of characters while StringBuffer and StringBuilder offer alternatives that allow you to modify the sequence.

It is similar to StringBuffer and String class except that this mutable object can be modified, whereas StringBuffer is immutable. The performance of StringBuilder is faster than StringBuffer since it does not support multiple threads; thus, it is non-synchronized.

Post On:StringBuilder Class In Java
Post Type:Java Tutorials
Published On:www.softwaretestingo.com
Applicable For:Freshers & Experience
Get Updates:SoftwareTestingo Telegram Group

In this tutorial, you will learn about the StringBuilder class and its methods like append, reverse, delete, and toString using examples.

What is StringBuilder in java?

If you have the word “SoftwareTestingo” in a particular memory location, it’s better to store it in the StringBuilder class instead of the String class. This is because when new characters are added, no new memory is allocated each time like it would be if you stored it in the String class. The StringBuilder class is mutable, which means updating can be done within the sequence itself.

If we want to perform character manipulation, it’s better to use the StringBuilder class instead of the String class. Let’s take a closer look at some of the features of the StringBuilder class.

Java Tutorial
  • Java String subsequence
  • Java String compareTo()
  • Java String substring
  • Java Split String
  • Java String concatenation

Syntax:

The StringBuilder class in Java allows you to create mutable strings. This means that you can change the value of a string after it has been created. This class extends the Object class, which is the parent class of all predefined and user-defined classes in Java.

The StringBuilder class contains methods like hashCode() and equals(), and implements the two interfaces Serializable and CharSequence. These features allow for different types of character sequences to be implemented.

StringBuilder Syntax
StringBuilder Syntax

How does StringBuilder Work Internally?

What will happen when the Buffer gets full? A new Buffer is automatically created that is double the size of the older buffer. No new memory is allocated for every update. Also, StringBuilder is not synchronized, which means multiple threads can access the StringBuilder object at the same time. Therefore, StringBuilder is not a thread-safe class.

StringBuilder Class Constructors

The StringBuilder class provides different types of constructors that help convert an ordinary sequence of characters to a StringBuilder format, and configure properties such as size. Let’s discuss the different constructors one by one.

StringBuilder(): Constructs a string builder with no characters in it and an initial capacity of 16 characters. The maximum number of characters that a StringBuilder object can store is known as the capacity of the StringBuilder.

Syntax:

StringBuilder obj=new StringBuilder();

Example:

package com.SoftwareTestingO.Strings;
public class StringBuilderEx 
{
	public static void main(String[] args) 
	{
		StringBuilder obj = new StringBuilder("Software Blog");
		obj.insert(8, "Testingo");

		System.out.println(obj);
	}
}

StringBuilder(int capacity): This constructor initializes a new instance of the StringBuilder class with no characters in it and the specified capacity. The following is the syntax for this constructor.

Syntax:

StringBuilder obj=new StringBuilder(10);
package com.SoftwareTestingO.Strings;
public class StringBuilderEx 
{
	public static void main(String[] args) 
	{
		StringBuilder obj = new StringBuilder(10);

		// print string
		System.out.println("Object capacity = "+ obj.capacity());
	}
}


StringBuilder(CharSequence seq): This constructor is used for creating a StringBuilder of char sequence. The char sequence is the sequence of characters in a string.

StringBuilder obj=new StringBuilder(CharSequence);
package com.SoftwareTestingO.Strings;
public class StringBuilderEx2 
{
	public static void main(String[] args) 
	{
		StringBuilder obj = new StringBuilder("AAAABBBCCCC");

		// print string
		System.out.println("String1 = "+ obj.toString());
	}
}

StringBuilder(String str): Constructs a string builder initialized to the contents of the specified string.

Syntax:

StringBuilder ob=new StringBuffer(String);
package com.SoftwareTestingO.Strings;
public class StringBuilderEx2 
{
	public static void main(String[] args) 
	{
		StringBuilder obj = new StringBuilder("AAAABBBCCCC");

		// using StringBuilder(String) constructor
		StringBuilder obj1= new StringBuilder(obj.toString());

		// print string
		System.out.println("Object 1 = "+ obj1.toString());
	}
}

Methods of StringBuilder Class

The StringBuilder class provides a variety of inbuilt methods that can be used to perform different operations on Strings. Let’s discuss each method one by one.

append(): This method attaches the passed string at the end of the exiting string.

This method takes one parameter. This method adds a specified value to the end of a StringBuilder sequence. The value can be any data type.

package com.SoftwareTestingO.Strings;
public class StringBuilderEx2 
{
	public static void main(String[] args) 
	{
		StringBuilder first = new StringBuilder("Software");
		first.append("Testingo");
		System.out.println("After Append: "+first);

		first.append(111);
		System.out.println("After Adding Different Datatype: "+first);
	}
}

Insert(): The capacity of a StringBuilder object can be found using this method. The default capacity of the StringBuilder class is 16 bytes. If the capacity is full then the new capacity will be current (capacity+1)*2.

package com.SoftwareTestingO.Strings;
public class StringBuilderEx3 
{
	public static void main(String[] args) 
	{
		StringBuilder first = new StringBuilder();
		System.out.println("Default Capacity: "+first.capacity());

		//Capacity increase by (16(default capacity)+1)*2=34
		first.append("aaaaaaaaaaaaaaaaaa");
		System.out.println("After Full, New Capacity: "+first.capacity());
	}
}

substring() method:

Converting The StringBuilder Object to a String

If you need to change your StringBuilder object into a regular ol’ string, never fear! The StringBuilder class provides a method, StringBuilder.toString(), which does just that.

package com.SoftwareTestingO.Strings;
public class StringBuilderEx4 
{
	public static void main(String[] args) 
	{
		StringBuilder first = new StringBuilder();
		first.append("SoftwareTestingo");
		
		//Convert Stringbuilder to String
		System.out.println(first.toString());
	}
}

What is the use of String builder?

The StringBuilder class is useful for storing mutable elements. This saves memory because no extra memory is used after updating the StringBuilder sequence.

Why do we use String builder in Java?

One of the main reasons is StringBuilder is not thread-safe which means When multiple threads can access a StringBuilder object, it increases performance because no thread has to wait for their turn for a particular operation.

Is String builder efficient?:

Yes, StringBuilder is efficient because due to the mutable and non-thread-safe property of the StringBuilder.

Which is better: StringBuilder or String?:

StringBuilder is better because it is 6000 times faster than the String class.

    Filed Under: Java Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Copyright © 2022 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers