StringBuilder class in Java

Introduction 

 
StringBuilder class in Java is used to create a mutable (modifiable) string or sequence of characters. This class is similar to StringBuffer class used in Java because it provides an API compatible with StringBuffer. But there is a difference between the two that StringBuilder class is a non-synchronized while StringBuffer class is synchronized.
 

Constructors

 
Here, the four constructors work for StringBuilder class as they also work for the StringBuffer class
  • StringBuilder() constructs an empty string builder with initial capacity of 16 characters.
  • StringBuilder(String str) constructs a string builder with specified string.
  • StringBuilder(int capacity or length) constructs an empty string builder with an initial capacity specified by capacity or length argument.
  • StringBuilder(CharSequence cs) constructs a string builder that contains the same characters as specified CharSequence.

Methods

 
The following are the  methods  used in StringBuilder as well as StringBuffer class.
  • append() : (String str), (char), (boolean), (int), (float), etc.
  • insert() : (int offset, String str), (int boolean), (int char), etc.
  • reverse()
  • replace() : (int startIndex, int endIndex, String str)
  • delete() : (int startIndex, int endIndex)
  • int capacity()
  • void ensureCapacity() : (int minimumCapacity)
  • char charAt() : (int index)
  • int length()
  • substring() : (int startIndex), (int startIndex, int endIndex)
  • indexOf() : (String str), (String str, int fromIndex)
  • lastIndexOf() : (String str), (String str, int fromIndex)
  • setChatAt() : (int index, char ch)
  • setLength() : (int newLength)
  • etc.
You can read the detail description of its constructors and methods along with easy illustrations for great understanding in my previous article series StringBuffer class.
 
The only thing you have to do is that you use StringBuilder when you read for the StringBuilder class instead of the StringBuffer class.
 
 
 
Thank you. Keep learning and sharing.