๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

StringBuilder Class in Java  


Introduction
  • The 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.
  • It was introduced in JDK 1.5 as a faster alternative to StringBuffer.
  • StringBuilder class is present in java.lang package.
  • Syntax :-
    package java.lang;
    
    public final class StringBuilder extends AbstractStringBuilder implements Serializable, CharSequence
    {
        // Fields
        // Constructors
        // Methods
    }
  • Unlike 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
Methods of StringBuilder Class:
  • Below are some common and important methods of 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
Properties of StringBuilder Class:
  1. Mutable:
    • StringBuilder objects are mutable, meaning their content can be changed after creation without creating a new object.
  2. Stored in Heap Memory:
    • StringBuilder objects are stored in heap memory, unlike string literals which are stored in the String Constant Pool (SCP).
  3. Non-Synchronized (Not Thread-Safe):
    • 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.
  4. Dynamic Resizing:
    • StringBuilder automatically increases its capacity as more characters are added, reducing overhead of frequent object creation.
  5. Unicode Support:
    • StringBuilder stores text as UTF-16 encoded Unicode characters, supporting multiple languages and symbols.