StringBuilder
class in Java represents a sequence of characters that can be modified after creation (mutable strings).
StringBuilder
is non-synchronized,
which means it is not thread-safe but offers better performance than StringBuffer
in single-threaded applications.
StringBuffer
.
StringBuilder
class is present in java.lang
package.
package java.lang;
public final class StringBuilder extends AbstractStringBuilder implements Serializable, CharSequence
{
// Fields
// Constructors
// Methods
}
String
, StringBuilder objects are mutable, meaning you can change their content without creating a new object.
StringBuilder sb = new StringBuilder("Deepak");
sb.append(" Panwar"); // Modifies the same object
System.out.println(sb); // Output: Deepak Panwar
StringBuilder
Class:
StringBuilder
class with examples:
Method | Description | Example | Output |
---|---|---|---|
append(String str) |
Adds the specified string to the end of the current string. |
StringBuilder sb = new StringBuilder("Hello"); sb.append(" World"); System.out.println(sb); |
Hello World |
insert(int offset, String str) |
Inserts the specified string at the given index. |
StringBuilder sb = new StringBuilder("Hello World"); sb.insert(6, "Java "); System.out.println(sb); |
Hello Java World |
replace(int start, int end, String str) |
Replaces characters from start index to end index with the given string. |
StringBuilder sb = new StringBuilder("Hello World"); sb.replace(6, 11, "Java"); System.out.println(sb); |
Hello Java |
delete(int start, int end) |
Deletes characters from start index to end index. |
StringBuilder sb = new StringBuilder("Hello Java"); sb.delete(5, 10); System.out.println(sb); |
Hello |
reverse() |
Reverses the sequence of characters in the builder. |
StringBuilder sb = new StringBuilder("Hello"); sb.reverse(); System.out.println(sb); |
olleH |
charAt(int index) |
Returns the character at the given index. |
StringBuilder sb = new StringBuilder("Hello"); System.out.println(sb.charAt(1)); |
e |
setCharAt(int index, char ch) |
Replaces the character at the specified index. |
StringBuilder sb = new StringBuilder("Hello"); sb.setCharAt(0, 'Y'); System.out.println(sb); |
Yello |
length() |
Returns the number of characters in the builder. |
StringBuilder sb = new StringBuilder("Hello"); System.out.println(sb.length()); |
5 |
capacity() |
Returns the current capacity (allocated storage). |
StringBuilder sb = new StringBuilder("Hello"); System.out.println(sb.capacity()); |
Typically 21 (16 default + 5) |
setLength(int newLength) |
Sets the length of the builder (truncates or adds null chars). |
StringBuilder sb = new StringBuilder("Hello World"); sb.setLength(5); System.out.println(sb); |
Hello |
substring(int start, int end) |
Returns a new String from start index to end index. |
StringBuilder sb = new StringBuilder("Hello World"); System.out.println(sb.substring(0, 5)); |
Hello |
StringBuilder
Class:
StringBuilder
objects are mutable, meaning their content can be changed after creation without creating a new object.
StringBuilder
objects are stored in heap memory, unlike string literals which are stored in the String Constant Pool (SCP).
StringBuilder
methods are not synchronized, so it is not safe in multi-threaded environments.
However, this makes it faster and more efficient than StringBuffer
in single-threaded applications.
StringBuilder
automatically increases its capacity as more characters are added, reducing overhead of frequent object creation.
StringBuilder
stores text as UTF-16 encoded Unicode characters, supporting multiple languages and symbols.
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weโre here to make our tutorials better based on your thoughts and suggestions.