StringBuffer Class in JavaLast Updated : 10 Jan 2026 Java StringBuffer class is thread-safe, i.e., multiple threads cannot access it simultaneously. So, it is safe and will result in an order. Constructors of the StringBuffer ClassThe StringBuffer class provides multiple constructors to create mutable string objects with different initial states and capacities. 1. StringBuffer()This constructor creates an empty StringBuffer object with a default initial capacity of 16 characters. Syntax: It has the following syntax: 2. StringBuffer(String str)This constructor creates a StringBuffer object initialized with the specified string. Syntax: It has the following syntax: 3. StringBuffer(int capacity)This constructor creates an empty StringBuffer object with the specified initial capacity. Syntax: It has the following syntax: ExampleThe following example demonstrates how to create StringBuffer objects using different constructors: Output: sb1 (default): "", Capacity: 16 sb2 (with string): "Hello", Capacity: 21 sb3 (with capacity 50): "", Capacity: 50 Explanation:
Methods of the StringBuffer classThe following tables shows the commonly used methods with their descriptions of the StringBuffer class:
StringBuffer Class Methods ExamplesHere are the StringBuffer class's methods along with appropriate examples. 1. StringBuffer Class append() MethodThe append() method concatenates the given argument with this String. The following example demonstrates the use of append() method. Output: Hello Java 2. StringBuffer insert() MethodThe insert() method inserts the given String into this string at the given position. The following example demonstrates the use of insert() method. Output: HJavaello 3. StringBuffer replace() MethodThe replace() method replaces the given String from the specified beginIndex and endIndex. The following example demonstrates the use of replace() method. Output: HJavalo 4. StringBuffer delete() MethodThe delete() method of the StringBuffer class deletes the String from the specified beginIndex to endIndex. The following example demonstrates the use of delete() method. Output: Hlo 5. StringBuffer reverse() MethodThe reverse() method of the StringBuffer class reverses the current String. The following example demonstrates the use of reverse() method. Output: olleH 6. StringBuffer capacity() MethodThe capacity() method of the StringBuffer class returns the current capacity of the buffer. The default capacity of the buffer is 16. If the number of characters 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. StringBuffer ensureCapacity() MethodThe ensureCapacity() method of the StringBuffer class ensures that the given capacity is the minimum of 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 ensureCapacity() method of the StringBuffer class guarantees that the specified capacity is at least the current capacity. If the specified capacity exceeds the current capacity, it increases the capacity to (oldcapacity* 2) + 2. For instance, if the current capacity is 16, it will be calculated as (16* 2) + 2 = 34. The following example demonstrates the use of ensureCapacity() method. Output: 16 16 34 34 70 Next TopicStringbuilder-in-java |
We request you to subscribe our newsletter for upcoming updates.