logo
Java String Buffer Methods
StringBuffer reverse( ):
This reverses the character sequence in the StringBuffer.

StringBuffer reverse program
class Test 
{ 
public static void main(String[] args) 
{ 
StringBuffer sb=new StringBuffer("abc"); 
System.out.println(sb); 
} 
}
Output :
output :     cba
StringBuffer append
x may be boolean, byte, int, long, float, double, char, character array, String or another StringBuffer. It will be added to StringBuffer object
StringBuffer append program
class Test 
{ 
public static void main(String[] args) 
{ 
StringBuffer sb=new StringBuffer("rattaiah"); 
String str=" salary "; 
int a=30000; 
sb.append(str); 
sb.append(a); 
System.out.println(sb);
}
}
Output :
output:rattaiah salary 30000
String Buffer Insert
By using above method we are able to insert the string any location of the existing string.
String Buffer Insert program
class Test 
{ 
public static void main(String[] args) 
{ 
StringBuffer sb=new StringBuffer("freetimelearn"); 
sb.insert(0,"hi "); 
System.out.println(sb); 
} 
}
Output :
hi freetimelearn
StringBuffer delete
This removes the characters from ith position till j-1th position in the StringBuffer.
String Buffer delete program
class test
{ 
public static void main(String[] args) 
{ 
StringBuffer sb=new StringBuffer("freetimelearn"); 
sb.delete(1, 3); 
System.out.println(sb); 
} 
}
Output :
freetimelearn
StringBuffer replace
This replaces characters from i to j-1, by the string str in StringBuffer object.
StringBuffer replace
class sup 
{ 
public static void main(String[] args) 
{ 
StringBuffer sb=new StringBuffer("freetimelearn"); 
sb.replace(1,2,"hi"); 
System.out.println(sb); 
} 
}
Output :
freetimelearn
String toString
This converts the StringBuffer object into a string object.
String toString program
class sup 
{ 
public static void main(String[] args) 
{ 
StringBuffer sb=new StringBuffer("freetimelearn"); 
String s=sb.toString(); 
System.out.println(s); 
} 
}
Output :
freetimelearn
StringBuilder :
Java.lang.StringBuilder :

1) Introduced in jdk1.5 version.
2) StringBuilder is identical to StringBuffer except for one important difference.
3) Every method present in the StringBuilder is not Synchronized means that is not thread safe.
4) multiple threads are allow to operate on StringBuilder methods hence the performance of the application is increased.