The knowledge sharing zone - Guruzon.com

Use of StringBuilder class with Example

This tutorial contains the details about StringBuilder Class Of Java, Like - What is use of StringBuilder class, Important keypoints about it with examples and useful APIs of StringBuilder.
reviews

The java.lang.StringBuffer and java.lang.StringBuilder classes should be used when you have to make a lot of modifications to strings of characters.

What is StringBuilder class in java

  • J2SE 5 adds a new string class to Java’s already powerful string handling capabilities, this new class is called StringBuilder. 
  • It is identical to StringBuffer except for one important difference: it is not synchronized, which means that it is not thread-safe.
  • The advantage of StringBuilder is faster performance. However, in cases in which you are using multithreading, you must use StringBuffer rather than StringBuilder.

Difference - StringBuffer vs StringBuilder

  • The StringBuilder class was added in Java 5.
  • It has exactly the same API as the StringBuffer class, except StringBuilder is not thread safe, In other words, its methods are not synchronized.
  • Sun recommends that you use StringBuilder instead of StringBuffer whenever possible because StringBuilder will run faster (and perhaps jump higher).
  • Apart from synchronization, anything we say about StringBuilder's methods holds true for StringBuffer's methods, and vice versa.

Use of StringBuilder in java

  • A common use for StringBuffers and StringBuilders is file I/O when large, ever-changing streams of input are being handled by the program.
  • In these cases, large blocks of characters are handled as units, and StringBuffer objects are the ideal way to handle a block of data, pass it on, and then reuse the same memory to handle the next block of data.

Important - Keypoint about StringBuilder

  • StringBuilder objects are mutable, allows us to make modify characters of the same object multiple times.
  • StringBuilder is Serializable, you can easily pass its objects over network of for storage.
  • StringBuilder initial capacity is of 16 characters when constructed with no characters in it.
  • StringBuilder will automatically grow to make room for additions of characters.
  • StringBuilder class APIs are not synchronized hence not thread safe, But it gives better performance than StringBuffer APIs.
  • StringBuilder are declared as final, you can not subclass it.
  • StringBuilder is very useful for large file I/O and ever-changing streams of input handling by the program.
  • StringBuilder objects are the ideal way to handle a block of data, pass it on, and then reuse the same memory to handle the next block of data.

StringBuilder Constructors

StringBuilder defines these four constructors:

1. The default constructor (the one with no parameters) reserves room for 16 characters without reallocation.
StringBuilder( )

2. The second version accepts an integer argument that explicitly sets the size of the buffer.
StringBuilder(int size)

3. The third version accepts a String argument that sets the initial contents of the StringBuilder object and reserves room for 16 more characters without reallocation. StringBuilder allocates room for 16 additional characters when no specific buffer length is requested, because reallocation is a costly process in terms of time. Also, frequent reallocations can fragment memory. By allocating room for a few extra characters, StringBuilder reduces the number of reallocations that take place.
StringBuilder(String str)

4. The fourth constructor creates an object that contains the character sequence contained in chars.
StringBuilder(CharSequence chars)

StringBuilder - length( ) versus capacity( )

  • The current length of a StringBuilder can be found via the length( ) method, while the total allocated capacity can be found through the capacity( ) method.
  • They have the following general forms:

   int length( )
   int capacity( )

Here is an example:

// StringBuilder length vs. capacity.
class StringBuilderDemo {
   public static void main(String args[]) {
      StringBuilder sb = new StringBuilder("Hello");
      System.out.println("buffer = " + sb);
      System.out.println("length = " + sb.length());
      System.out.println("capacity = " + sb.capacity());
   }
}

Output:
   buffer = Hello
   length = 5
   capacity = 21

/**

   Since sb is initialized with the string “Hello” when it is created, its length is 5. Its capacity is 21 because room for 16 additional characters is automatically  added.

**/

 

FAQ about StringBuilder


What is use of StringBuilder class in java?


When to use String, StringBuilder and StringBuffer in java?


What is difference between StringBuffer and StringBuilder?


Is StringBuilder also uses String literal pool or constant pool mechanism?


Is StringBuilder mutable or immmutable?


Is StringBuilder is thread safe?


 

You would also like to read:

String vs StringBuffer vs StringBuilder classes
Use of StringBuffer class with Example
String class & String literal pool in java

Comments :

Name :
Email :
Comment :
Verify Text :
capch image, refresh page if not loaded somehow