Last Updated: 18 September, 2023
StringBuffer is a mutable, final, and synchronized (thread-safe) Java class that was introduced in the JDK 1.0 version and is available in the java.lang package.
The StringBuffer class is used to create a mutable String object (the object can be changed once created). StringBuffer is similar to the String class except for one major difference: StringBuffer is mutable, whereas String is immutable (an object, once created, cannot be changed).
The StringBuffer class is thread-safe, which means that at any time, multiple threads cannot access it simultaneously. So it is safe and will result in order, but implementing the synchronization mechanism will take some time for the JVM, which is why StringBuffer is a little slower than StringBuilder.
The StringBuffer class provides methods to append, insert, delete, and replace characters within the buffer.
Java StringBuffer Class Declaration
public final class StringBuffer
extends Object
implements Serializable, Comparable <StringBuffer>, CharSequence
Example: StringBuffer class Implementation in Java
Output
Welcome in StringBuffer Class
Constructor | Description |
---|---|
StringBuffer() | Constructs a string buffer with no characters in it and an initial capacity of 16 characters. |
StringBuffer(CharSequence seq) | Constructs a string buffer that contains the same characters as the specified CharSequence. |
StringBuffer(int capacity) | Constructs a string buffer with no characters in it and the specified initial capacity. |
StringBuffer(String str) | Constructs a string buffer initialized to the contents of the specified string. |
Syntax: public int length()
The length() method returns the total length of the StringBuffer object. This method always returns an integer value as an output. If the StringBuffer object is empty, it returns 0.
Output
StringBuffer length : 29
Syntax: public StringBuffer reverse()
The reverse() method is used to replace the character sequence with its reverse order.
Output
StringBuffer Reverse : ssalC reffuBgnirtS ni emocleW
Syntax: public String substring(int start, int end)
This method returns a new string that contains a subsequence of characters currently contained in this sequence. The substring starts at the specified start and extends to the character at index end -1.
Output
StringBuffer substring : ome in S
Syntax: public char charAt(int index)
This method returns the char value in the sequence at the specified index. The index start from 0 same as array. If given index value beyound the range then will get IndexOutOfBoundsException.
Output
StringBuffer charAt() : m
Syntax: public int capacity()
This method returns the current capacity. The capacity is the quantity of storage that is available for newly added characters; once this number has been exceeded, an allocation will take place.
Output
StringBuffer capacity : 45
Syntax: public StringBuffer append( Object obj)
Appends the specified string to this character sequence.
Output
Before append : Welcome
After append : Welcome in StringBuffer Class
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
StringBuffer Vs StringBuilder in Java
Ans. StringBuffer and StringBuilder classes are used to represent a sequence of characters in Java, and both classes are by nature final and mutable objects. There are the following differences between these two classes:
StringBuffer | StringBuilder |
---|---|
The StringBuffer object is thread-safe and synchronized. Multiple threads cannot call the methods of StringBuffer simultaneously. | The StringBuilder object is not synchronized and not thread-safe. Multiple threads can call the methods of StringBuilder simultaneously. |
StringBuffer is suitable for multi-threaded environments. | StringBuilder is suitable for single-threaded environments. |
StringBuffer performance is slower as compared to StringBuilder. | StringBuilder performance is faster as compared to StringBuffer. |
StringBuffer has been available since the JDK 1.0 version. | StringBuilder has been available since JDK Version 5. |
How to convert a StringBuffer into a StringBuilder in Java?
Ans. To convert StringBuffer to StringBuiler, first we need to convert StringBuffer to a String object by using the toString() method, and then we will create StringBuilder and pass the String object into the constructor.
Let's see an example:
public class ConvertBufferToBuilder { public static void main(String[] args) { StringBuffer buffer = new StringBuffer("JavaByTechie"); // STEP: 1 // Conversion from StringBuffer object to the String object String str = buffer.toString(); // STEP: 2 // Creating a StringBuilder and passing String object (str) into the constructor StringBuilder builder = new StringBuilder(str); System.out.println("StringBuilder value: " + builder); } }
Output
How to convert a StringBuilder into a StringBuffer in Java?
Ans. Same as the StringBuffer to StringBuiler conversion process, first we need to convert StringBuilder to a String object by using the toString() method, and then we will create StringBuffer and pass the String object into the constructor.
Let's see an example:
public class ConvertBuilderToBuffer { public static void main(String[] args) { StringBuilder builder = new StringBuilder("JavaByTechie"); // STEP: 1 // Conversion from StringBuilder object to the String object String str = builder.toString(); // STEP: 2 // Creating a StringBuffer and passing String object (str) into the constructor StringBuffer buffer = new StringBuffer(str); System.out.println("StringBuffer value: " + buffer); } }
Output
String Vs StringBuffer in Java.
Ans. String and StringBuffer are final classes of the java.lang package and are used to store a sequence of characters. There are many differences between them. Let's look at the given table:
String | StringBuffer |
---|---|
A string is an immutable class, which means its values cannot be changed once created. If we modify the string, the JVM will create another object. | A StringBuffer is a mutable class. An object can be changed once it is created. |
Strings can be created in two ways: 1. by string literal 2. by using a new keyword | There is only one way to create a StringBuffer: using a new keyword. |
In concatenation operations, a string is slow and consumes more memory because it creates a new instance every time. | whereas StringBuffer is fast and consumes less memory. |
String uses a "string constant pool" and heap memory for storing the string object. | The StringBuffer only uses heap memory. |
There is no capacity concept in strings. | StringBuffer has a default capacity of 16. |
Is the Stringbuffer class thread safe?
Yes, StringBuffer is thread-safe and synchronized. in Java.
Why is StringBuffer slower than StringBuilder?
StringBuffer is by default synchronized, whereas StringBuilder is not synchronized in Java. Implementing the synchronisation mechanism takes some time for the JVM. Hence, StringBuffer takes more execution time than StringBuilder, so StringBuffer is slower than StringBuilder.
What are the common features of the StringBuffer and StringBuilder classes?
Ans. StringBuffer and StringBuilder both have the following common features:
How to convert a String into StringBuffer and vice versa?
Ans.
public class StringToStringBufferAndViceVersa { public static void main(String[] args) { StringBuffer sb = new StringBuffer(); String str1 = new String("String is an object in Java."); String str2 = "String is a sequance of characters"; // Converting String into StringBuffer sb.append(str1); sb.append(str2); // Converting StringBuffer into String String str = sb.toString(); System.out.println("String value: " + str); System.out.println("StringBuffer value: " + sb); } }
Output
String value: String is an object in Java.String is a sequance of characters
StringBuilder value: String is an object in Java.String is a sequance of characters