Java StringBuffer class
Java StringBuffer class is used to create mutable (modifiable) string. The StringBuffer class in java is same as String class except it is mutable i.e. it can be changed.
Note: Java StringBuffer class is thread-safe i.e. multiple threads cannot access it simultaneously. So it is safe and will result in an order.
What is mutable string
A string that can be modified or changed is known as mutable string. StringBuffer and StringBuilder classes are used for creating mutable string.
1) StringBuffer append() method
The append() method concatenates the given argument with this string.
- class StringBufferExample{
- public static void main(String args[]){
- StringBuffer sb=new StringBuffer("Hello ");
- sb.append("Java");//now original string is changed
- System.out.println(sb);//Hello Java
- }
- }
2) StringBuffer insert() method
The insert() method inserts the given string with this string at the given position.
- class StringBufferExample2{
- public static void main(String args[]){
- StringBuffer sb=new StringBuffer("Hello ");
- sb.insert(1,"Java");//now original string is changed
- System.out.println(sb);//prints HJavaello
- }
- }
3) StringBuffer replace() method
The replace() method replaces the given string from the specified beginIndex and endIndex.
- class StringBufferExample3{
- public static void main(String args[]){
- StringBuffer sb=new StringBuffer("Hello");
- sb.replace(1,3,"Java");
- System.out.println(sb);//prints HJavalo
- }
- }
4) StringBuffer delete() method
The delete() method of StringBuffer class deletes the string from the specified beginIndex to endIndex.
- class StringBufferExample4{
- public static void main(String args[]){
- StringBuffer sb=new StringBuffer("Hello");
- sb.delete(1,3);
- System.out.println(sb);//prints Hlo
- }
- }
5) StringBuffer reverse() method
The reverse() method of StringBuilder class reverses the current string.
- class StringBufferExample5{
- public static void main(String args[]){
- StringBuffer sb=new StringBuffer("Hello");
- sb.reverse();
- System.out.println(sb);//prints olleH
- }
- }
6) StringBuffer capacity() method
The capacity() method of StringBuffer class returns the current capacity of the buffer. The default capacity of the buffer 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
- class StringBufferExample6{
- public static void main(String args[]){
- StringBuffer sb=new StringBuffer();
- System.out.println(sb.capacity());//default 16
- sb.append("Hello");
- System.out.println(sb.capacity());//now 16
- sb.append("java is my favourite language");
- System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2
- }
- }
No comments:
Post a Comment