We will find an answer for the following questions

  • What is StringBuilder
  • Available Constructors
  • Commonly used methods
  • Java StringBuilder class is used to create a mutable (modifiable) string.
  • The Java StringBuilder class is the same as the StringBuffer class except that it is non-synchronized.
  • So its not thread-safe and can’t be used if Objects are shared among multiple threads.
  • It is available since JDK 1.5.
  • If sb refers to an instance of a StringBuilder, then sb.append(x) has the same effect as sb.insert(sb.length(), x)
// creates an empty string Builder with the initial capacity of 16.
StringBuilder str = new StringBuilder();

// creates a string Builder with the specified string.
StringBuilder str = new StringBuilder("Hello World");

// creates an empty string Builder with the specified capacity as length.
StringBuilder str = new StringBuilder(40);
Commonly used Methods
// used to append the specified string with this string. 
// The append() method is overloaded like append(char),
// append(boolean), append(int), append(float), append(double) etc.
str.append(" from India");

// reverse the order of characters

// Convert to String
String data = str.toString();

// return the length of the string

// clears object
str = new StringBuilder();


Categories: JAVA


Leave a Reply

Avatar placeholder

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