StringBuilder Class in JavaLast Updated : 10 Jan 2026 Java StringBuilder class is used to create mutable (modifiable) String. The Java StringBuilder class is same as StringBuffer class except that it is non-synchronized. It is available since JDK 1.5. Import Statement for StringBuilder ClassThe StringBuilder class belongs to the java.lang package that is automatically imported in every Java program. Therefore, no explicit import statement is required to use StringBuilder. SyntaxThe below syntax demonstrating directly use of StringBuilder in your program without adding any import statement: Constructors of StringBuilder ClassThe StringBuilder class provides multiple constructors to create mutable string objects with different initial capacities or values. 1. StringBuilder() ConstructorThis constructor creates an empty StringBuilder object with a default initial capacity of 16 characters. Syntax: It has the following syntax: It is useful when you do not know the initial content size and want a flexible buffer. 2. StringBuilder(String str) ConstructorThis constructor creates a StringBuilder object initialized with the specified string. Syntax: It has the following syntax: 3. StringBuilder(int length) ConstructorThis constructor creates an empty StringBuilder object with the specified initial capacity. Syntax: It has the following syntax: It is helpful when the required capacity is known in advance to improve performance. Methods of StringBuilder ClassThe following table shows the commonly used method of StringBuilder class:
Java StringBuilder ExamplesLet's see the examples of different methods of StringBuilder class. 1. StringBuilder append() methodThe StringBuilder append() method concatenates the given argument with this String. The following example demonstrates the use of append() method. Output: Hello Java 2. StringBuilder insert() methodThe StringBuilder insert() method inserts the given string with this string at the given position. The following example demonstrates the use of insert() method. Output: HJavaello 3. StringBuilder replace() methodThe StringBuilder replace() method replaces the given string from the specified beginIndex and endIndex. The following example demonstrates the use of replace() method. Output: HJavalo 4. StringBuilder delete() methodThe delete() method of StringBuilder class deletes the string from the specified beginIndex to endIndex. The following example demonstrates the use of delete() method. Output: Hlo 5. StringBuilder reverse() methodThe reverse() method of StringBuilder class reverses the current string. The following example demonstrates the use of reverse() method. Output: olleH 6. StringBuilder capacity() methodThe capacity() method of StringBuilder class returns the current capacity of the Builder. The default capacity of the Builder is 16. If the number of character increases from its current capacity, it increases the capacity by (oldcapacity*2)+2. For example if your current capacity is 16, it will be (16*2)+2=34. The following example demonstrates the use of capacity() method. Output: 16 16 34 7. StringBuilder ensureCapacity() methodThe ensureCapacity() method of StringBuilder class ensures that the given capacity is the minimum to the current capacity. If it is greater than the current capacity, it increases the capacity by (oldcapacity*2)+2. For example if your current capacity is 16, it will be (16*2)+2=34. The following example demonstrates the use of ensureCapacity() method. Output: 16 16 34 34 70 |
We request you to subscribe our newsletter for upcoming updates.