Java StringBuilder With Examples

Java StringBuilder class is mutable sequence of characters. StringBuilder Class can be comparable to String however the StringBuilder class provides more versatility because of its modification features.

Java StringBuilder

  • StringBuilder class provides an API similar to StringBuffer, but unlike StringBuffer, it doesn’t guarantee thread safety.
  • Java StringBuilder class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case).
  • If execution speed and performance is a factor, StringBuilder class can be used in place of StringBuffer.
  • The bread-and-butter operations provided by the StringBuilder Class are the append() and insert() methods. These methods are overloaded within StringBuilder in order to accommodate different data type.
  • The general process flow of StringBuilder append and insert methods is: (1) converts a given data to a string then (2) appends or inserts the characters of that string to the string builder. Java StringBuilder append() method always adds these characters at the end of the builder; insert() method inserts character(s) at a specified point.

StringBuilder Class Diagram

StringBuilder-Class-Diagram

StringBuffer and StringBuilder

StringBuffer StringBuilder
Synchronized, hence thread safe. Not synchronized, not thread safe.
Operates slower due to thread safety feature Better performance compared to StringBuffer
Has some extra methods – substring, length, capacity etc. Not needed because these methods are present in String too.
Introduced in Java 1.2 Introduced in Java 1.5 for better performance.

Java StringBuilder Constructors

Constructor Description
StringBuilder() Creates an empty string builder with a default capacity of 16 (16 empty elements).
StringBuilder(CharSequence cs) Constructs a string builder containing the same characters as the specified CharSequence, plus an extra 16 empty elements trailing the CharSequence.
StringBuilder(int initCapacity) Creates an empty string builder with the specified initial capacity.
StringBuilder(String s) Creates a string builder whose value is initialized by the specified string, plus an extra 16 empty elements trailing the string.

StringBuilder Length and Capacity

Java StringBuilder class, much like the String class, has length() method that returns the length of the character sequence in the builder.

However, StringBuilder inherits capacity() method from its superclass AbstractStringBuilder, that returns the number of character spaces that have been allocated. The returned value is always greater than or equal to the length (usually greater than) and automatically expands whenever necessary to accommodate character additions to the string builder.

There are couple of other methods related to StringBuilder length and capacity.

  1. void setLength(int newLength): Sets the length of the character sequence. If newLength is less than length(), the last characters in the character sequence are truncated. If newLength is greater than length(), null characters are added at the end of the character sequence.
  2. void ensureCapacity(int minCapacity): Ensures that the capacity is at least equal to the specified minimum.

StringBuilder methods like append(), insert() or setLength() can increase the length of the character sequence in the string builder so that the returned value of length() would be greater than the current capacity(). In this case, the capacity is automatically increased.

Java StringBuilder Example

Let’s see the examples of different methods of StringBuilder class.

  1. append(): The StringBuilder append() method concatenates or attaches the passed String argument with the existing declared string. It attaches it after the declared string.

    Output: Hello World
  2. insert(): StringBuilder insert() method inserts the passed String argument at the passed String index.
  3. replace(int startIndex, int endIndex, String str): StringBuilder replace() method replaces the existing declared string. String replacement occurs from the passed startingIndex up to the endingIndex.
  4. delete(int startIndex, int endIndex): StringBuilder delete() method deletes a character or sets of characters. Deletion occurs at passed startingIndex up to endingIndex.
  5. reverse(): The reverse() method of StringBuilder class reverses the existing declared string. Invoking a reverse() method on a StringBuilder object with no existing declared value throws NullPointerException.
  6. capacity(): The capacity() method of StringBuilder class returns the current capacity of the StringBuilder object. The default capacity of the Builder is 16. If the number of character increases from its current capacity, it increases the capacity by (old_capacity*2)+2 e.g. at current capacity 16, it becomes (16*2)+2=34.
  7. ensureCapacity(): The ensureCapacity() method of StringBuilder class ensures that the given capacity is the minimum to the current capacity. If it is greater than the current capacity, it increases the capacity by (old_capacity*2)+2 e.g. at current capacity 16, it becomes (16*2)+2 which is 34.

That’s all for Java StringBuilder class. It’s a very useful class to work with Strings in java.

Reference: API Doc

By admin

Leave a Reply