13 Jul StringBuffer class in Java
StringBuffer in Java is used to create modifiable strings i.e. it doesn’t have a fixed length like the String class in Java. The StringBuffer class has growable and writable character sequences. It is thread safe i.e. multiple threads cannot simultaneously access the same method.
Let us see how to create a string using StringBuffer:
1 2 3 |
StringBuffer strBuff = new StringBuffer("dwa"); |
StringBuffer vs. String
The String class has fixed length i.e. cannot be changed, whereas StringBuffer is growable and creates modifiable (mutable) strings. Let us now see the differences:
StringBuffer class Constructors
The constructors of StringBuffer are displayed below:
StringBuffer class Methods
The methods of StringBuffer displayed below with description:
For complete reference of the StringBuffer methods, refer the Oracle Java StringBuffer docs.
StringBuffer Examples
Following are some of the examples of StringBuffer:
Append the string representation
The append() method in Java appends the string representation. Here append(char ch) appends the string representation of the char argument to this sequence:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.lang.*; public class StudyopediaDemo { public static void main(String[] args) { StringBuffer strBuff = new StringBuffer("Dwa"); System.out.println("StringBuffer = " + strBuff); // append strBuff.append('y'); System.out.println("Updated = " + strBuff); // append strBuff.append('n'); System.out.println("Updated = " + strBuff); // append strBuff.append('e'); System.out.println("Final StringBuffer = " + strBuff); } } |
The output is as follows:
1 2 3 4 5 6 |
StringBuffer = Dwa Updated = Dway Updated = Dwayn Final StringBuffer = Dwayne |
Find the current capacity
To fetch the current capacity, the capacity() method is used in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.lang.*; public class StudyopediaDemo { public static void main(String[] args) { StringBuffer strBuff = new StringBuffer("Dwa"); System.out.println("StringBuffer = " + strBuff); // capacity will be 16 + 3 System.out.println("capacity = " + strBuff.capacity()); // append strBuff.append('y'); System.out.println("Updated = " + strBuff); } } |
The output is as follows:
1 2 3 4 5 |
StringBuffer = Dwa capacity = 19 Updated = Dway |
Reverse the value of the StringBuffer object
To reverse the value of the StringBuffer object, the reverse() method in Java is to be used:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import java.lang.*; public class StudyopediaDemo { public static void main(String[] args) { StringBuffer strBuff = new StringBuffer("Dwa"); System.out.println("StringBuffer = " + strBuff); // capacity will be 16 + 3 System.out.println("capacity = " + strBuff.capacity()); // append strBuff.append('y'); System.out.println("Updated = " + strBuff); // append strBuff.append('n'); System.out.println("Updated = " + strBuff); // append strBuff.append('e'); System.out.println("Final StringBuffer = " + strBuff); System.out.println("Final StringBuffer (Reverse)= " + strBuff.reverse()); } } |
The output is as follows:
1 2 3 4 5 6 7 8 |
StringBuffer = Dwa capacity = 19 Updated = Dway Updated = Dwayn Final StringBuffer = Dwayne Final StringBuffer (Reverse)= enyawD |
No Comments