String builder in Java is a class. It is like string objects, except that they can be modified. String Builder class is available in java.lang package. Internally String builder objects are treated like variable length arrays which contain sequence of characters. At any point of time, the length and content of the character sequence can be changed through the method invocation. Strings in java cannot modified.
For performance wise string builder is better, because it has no synchronized methods so it works faster and when we are doing number of string operations in a single thread we gain tremendous than string buffer.
It is available since JDK 1.5.
String Builder class declaration is:
public final class StringBuilder
extends Object
implements Serializable, CharSequence
String builder class has length and capacity. Unlike strings, every string builder also has a capacity, the number of character spaces that have been allocated. The capacity, which is returned by the capacity() method, is always greater than or equal to the length (usually greater than) and will automatically expand as necessary to accommodate additions to the string builder.
String builder class has length and capacity. Unlike strings, every string builder also has a capacity, the number of character spaces that have been allocated. The capacity, which is returned by the capacity() method, is always greater than or equal to the length (usually greater than) and will automatically expand as necessary to accommodate additions to the string builder.
String Builder –Constructor:
Constructor | Description |
StringBuilder() | Creates an empty string builder with a 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. |
Methods in String Builder:
Method | Description |
StringBuilder append(boolean b) StringBuilder append(char c) StringBuilder append(char[] str) StringBuilder append(char[] str, int offset, int len) StringBuilder append(double d) StringBuilder append(float f) StringBuilder append(int i) StringBuilder append(long lng) StringBuilder append(Object obj) StringBuilder append(String s) | Appends the argument to this string builder. The data is converted to a string before the append operation takes place. |
StringBuilder delete(int start, int end) StringBuilder deleteCharAt(int index) | The first method deletes the subsequence from start to end-1 (inclusive) in the StringBuilder’s char sequence. The second method deletes the character located at index. |
StringBuilder insert(int offset, boolean b) StringBuilder insert(int offset, char c) StringBuilder insert(int offset, char[] str) StringBuilder insert(int index, char[] str, int offset, int len) StringBuilder insert(int offset, double d) StringBuilder insert(int offset, float f) StringBuilder insert(int offset, int i) StringBuilder insert(int offset, long lng) StringBuilder insert(int offset, Object obj) StringBuilder insert(int offset, String s) | Inserts the second argument into the string builder. The first integer argument indicates the index before which the data is to be inserted. The data is converted to a string before the insert operation takes place. |
StringBuilder replace(int start, int end, String s) void setCharAt(int index, char c) | Replaces the specified character(s) in this string builder. |
StringBuilder reverse() | Reverses the sequence of characters in this string builder. |
String toString() | Returns a string that contains the character sequence in the builder. |
Note: String builder is not thread safe. In real world string builder is used when working with large files.
String Builder Demo program:
In the below program I demonstrate all methods in String Builder.
public class StringBuilderDemo
{
public static void main(String[] args)
{
StringBuilder sb = new StringBuilder(“Hello! string builder demo “);
System.out.println(“string builder Sb capacity is”+sb.capacity());
StringBuilder sb1 = new StringBuilder();
System.out.println(“String builder sb1 (empty) capacity is”+sb1.capacity());
sb.append(“Hai”);
System.out.println(“After append method executes ” +sb);
sb.insert(1,”Java”);
System.out.println(“After Insert method executes ” +sb);
sb.replace(2,4,”Java”);
System.out.println(“After replace method executes ” +sb);
sb.delete(3,6);
System.out.println(“After delete method executes ” +sb);
sb.reverse();
System.out.println(“After reverse method executes ” +sb);
System.out.println(sb);
System.out.println(“Now the capacity of the string builder is:”+sb.capacity());
}
}
Note: The above program implement and tested in my dev environment.
Output is:
string builder Sb capacity is43
String builder sb1 (empty) capacity is16
After append method executes Hello! string builder demo Hai
After Insert method executes HJavaello! string builder demo Hai
After replace method executes HJJavaaello! string builder demo Hai
After delete method executes HJJaello! string builder demo Hai
After reverse method executes iaH omed redliub gnirts !olleaJJH
iaH omed redliub gnirts !olleaJJH
Now the capacity of the string builder is:43