Java for Everyone... StringBuilder & StringBuffer
StringBuilder?objects are like?String?objects, except that they can be modified. Internally, these objects are treated like variable-length arrays that contain a sequence of characters. At any point, the length and content of the sequence can be changed through method invocations, like the?String?class, has a?length()?method that returns the length of the character sequence in the builder.
Constructor Summary
StringBuilder()-Constructs a string builder with no characters in it and an initial capacity of 16 characters.
StringBuilder(CharSequence seq)-Constructs a string builder that contains the same characters as the specified CharSequence.
StringBuilder(int capacity)-Constructs a string builder with no characters in it and an initial capacity specified by the capacity argument.
StringBuilder(String str)-Constructs a string builder initialized to the contents of the specified string.
Important Method Summary
append(char c)-Appends the string representation of the char argument to this sequence.
append(char[] str)-Appends the string representation of the char array argument to this sequence.
append(CharSequence s)-Appends the specified CharSequence to this sequence.
append(double d)-Appends the string representation of the double argument to this sequence.
append(String str)-appends the specified string to this character sequence.
appendCodePoint(int codePoint)-Appends the string representation of the codePoint argument to this sequence.
insert(int offset, char c)-Inserts the string representation of the char argument into this sequence.
replace(int start, int end, String str)-Replaces the characters in a substring of this sequence with characters in the specified String.
reverse()-Causes this character sequence to be replaced by the reverse of the sequence.
substring(int start)-Returns a new String that contains a subsequence of characters currently contained in this character sequence.
substring(int start, int end)-Returns a new String that contains a subsequence of characters currently contained in this sequence.
toString() Returns a string representing the data in this sequence.
You can follow this official website link for more details on Stringbuilder method & classes.
Examples-
领英推荐
public static void main(String[] args) {
StringBuilder sb1 = new StringBuilder("Hello ,Java");
// Append
sb1.append(" StringBuffer!");
System.out.println("After append: " + sb1); // Output: Hello ,Java StringBuffer!
// Insert
sb1.insert(25, " Welcome");
System.out.println("After insert: " + sb1); // Output: Hello ,Java StringBuffer! Welcome
// Replace
sb1.replace(12, 24, "World");
System.out.println("After replace: " + sb1); // Output: Hello ,Java World! Welcome
// Delete
sb1.delete(11, 17);
System.out.println("After delete: " + sb1); // Output: Hello ,Java! Welcome
// Reverse
sb1.reverse();
System.out.println("After reverse: " + sb1); // Output: emocleW !avaJ, olleH
}
Key Characteristics of StringBuilder
StringBuilder is a class in Java designed for creating and manipulating mutable strings.
Example-
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hey");
sb.append(" this").append(" Java");
System.out.println(sb); // Output: Hey this Java
}
StringBuffer
StringBuffer is a class in Java used to create mutable (modifiable) strings. Unlike the String class, where objects are immutable (cannot be changed once created), StringBuffer objects can be modified without creating new instances.
StringBuffer is thread-safe, as its methods are synchronized, making it suitable for multi-threaded applications. However, this synchronization can make it slower compared to StringBuilder
Basic programming example-
StringBuffer sb1 = new StringBuffer("Hello ,Java");
// Append
sb1.append(" StringBuffer!");
System.out.println("After append: " + sb1); // Output: Hello ,Java StringBuffer!
// Insert
sb1.insert(25, " Welcome");
System.out.println("After insert: " + sb1); // Output: Hello ,Java StringBuffer! Welcome
// Replace
sb1.replace(12, 24, "World");
System.out.println("After replace: " + sb1); // Output: Hello ,Java World! Welcome
// Delete
sb1.delete(11, 17);
System.out.println("After delete: " + sb1); // Output: Hello ,Java! Welcome
// Reverse
sb1.reverse();
System.out.println("After reverse: " + sb1); // Output: emocleW !avaJ, olleH
}
Key Characteristics of StringBuffer:
Mutable Strings: Strings can be modified in place (e.g., appending, deleting, or replacing characters).
Thread-Safe: Methods of StringBuffer are synchronized, making it thread-safe for multi-threaded environments. However, this comes with a performance trade-off compared to StringBuilder.
Performance: It is faster than String when there are multiple string modifications but slower than StringBuilder due to synchronization.
Key Differences Between StringBuffer and StringBuilder in Java
/*
This is article is based on JDK 8, any changes on features, enhancement or deprecated options can be referred to official release notes-
*/