StringBuilder and StringBuffer are companion classes for the String class.
They represent a mutable sequence of characters.
StringBuffer is thread-safe and StringBuilder is not thread-safe.
Both classes have the same methods, except that all methods in StringBuffer are synchronized.
A StringBuilder object is a modifiable string. The StringBuilder class contains four constructors:
StringBuilder() StringBuilder(CharSequence seq) StringBuilder(int capacity) StringBuilder(String str)
The no-args constructor creates an empty StringBuilder with a default capacity of 16.
The second constructor takes a CharSequence object as an argument.
It creates a StringBuilder object, whose content is the same as the specified CharSequence.
The third constructor takes an int as argument; it creates an empty StringBuilder object whose initial capacity is the same as the specified argument.
The following are some examples of creating StringBuilder objects:
StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder("Here is the content"); StringBuilder sb3 = new StringBuilder(200);
The append() method adds text to the end of the StringBuilder. It takes many types of arguments.
insert() and delete() modify its content, too.
The StringBuilder class has two properties: length and capacity.
Its length refers to the length of its content whereas its capacity refers to the maximum number of characters it can hold without allocating new memory.
The length() and capacity() methods return its length and capacity, respectively. For example,
public class Main { public static void main(String[] args) { StringBuilder sb = new StringBuilder(200); // Capacity:200, length:0 sb.append("Hello"); // Capacity:200, length:5 int len = sb.length(); // len is assigned 5 int capacity = sb.capacity(); // capacity is assigned 200 } }
We can get the content of a StringBuilder as a String by using its toString() method.
public class Main { public static void main(String[] args) { // Create a String object String s1 = new String("Hello"); //from w w w . j a v a 2 s . co m // Create a StringBuilder from of the String object s1 StringBuilder sb = new StringBuilder(s1); // Append " Java" to the StringBuilder's content sb.append(" Java"); // Now, sb contains "Hello Java" // Get a String from the StringBuilder String s2 = sb.toString(); // s2 contains "Hello Java" } }
StringBuilder has a setLength() method, which takes its new length as an argument. If the new length is greater than the old length, the extra positions are filled with null characters (a null character is \u0000).
If the new length is less than the old length, its content is truncated to fit in the new length.
public class Main { public static void main(String[] args) { // Length is 5 StringBuilder sb = new StringBuilder("Hello"); // Now the length is 7 with last two characters as null character '\u0000' sb.setLength(7); // Now the length is 2 and the content is "He" sb.setLength(2); } }
The StringBuilder class has a reverse() method, which replaces its contents with the same sequence of characters, but in reverse order.
The following code shows the use of some of the methods of the StringBuilder class.
public class Main { public static void main(String[] args) { // Create an empty StringBuffer StringBuilder sb = new StringBuilder(); printDetails(sb);//from w w w . j ava2s . c o m // Append "good" sb.append("good"); printDetails(sb); // Insert "Hi " in the beginning sb.insert(0, "Hi "); printDetails(sb); // Delete the first o sb.deleteCharAt(1); printDetails(sb); // Append " be with you" sb.append(" be with you"); printDetails(sb); // Set the length to 3 sb.setLength(3); printDetails(sb); // Reverse the content sb.reverse(); printDetails(sb); } public static void printDetails(StringBuilder sb) { System.out.println("Content: \"" + sb + "\""); System.out.println("Length: " + sb.length()); System.out.println("Capacity: " + sb.capacity()); // Print an empty line to separate results System.out.println(); } }
The code above generates the following result.
We often use the + operator to concatenate a strings, and a primitive type value or an object to another string.
For example,
String str = "X" + "Y" + 12.56;
To optimize the string concatenation operation, the compiler replaces the string concatenation by a statement, which uses a StringBuilder.
String str = new StringBuilder().append("X").append("Y").append(12.56).toString();