StringBuilder class in Java

StringBuilder class in Java is used to create a mutable (modifiable) string or sequence of characters. This class is similar as 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.
Here, the four constructors work for StringBuilder class as they also work for 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 string builder that contains same characters as specified CharSequence.
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 about 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 StringBuilder class instead of StringBuffer class.
Thank you, keep learning and sharing. 
Ebook Download
View all
View all