public final class StringBuffer
extends Object
implements Serializable, CharSequence
difference between String and StringBuffer
class test { public static void main(String[] args) { String str = "Java"; str.concat("Race"); System.out.println(str); //output: Java StringBuffer str = new StringBuffer("Java"); strB.append("Race"); System.out.println(str); //outpuut: JavaRace } }
Output is such because String objects are immutable objects. Hence, if we concatenate on the same String object, it won't be changed(Output: Java). But StringBuffer creates mutable objects. Hence, it can be changed(Output: JavaRace). Important methods of StringBuffer class
The following methods are some most commonly used methods of StringBuffer class.
This method will concatenate the string representation of any type of data to the end of the invoking StringBuffer object. append() method has several overloaded forms.
StringBuffer append(String str) StringBuffer append(int n) StringBuffer append(Object obj)
The string representation of each parameter is appended to StringBuffer object.
StringBuffer str = new StringBuffer("SILAN"); str.append("Software"); System.out.println(str);
This method inserts one string into another. Here are few forms of insert() method.
StringBuffer insert(int index,String str) StringBuffer insert(int index,int num) StringBuffer insert(int index,Object obj)
Here the first parameter gives the index at which position the string will be inserted and string representation of second parameter is inserted into StringBuffer object.
StringBuffer str = new StringBuffer("Java"); str.insert(4, 12345); System.out.println(str);
Output : Java12345
This method reverses the characters within a StringBuffer object.
StringBuffer str = new StringBuffer("JavaRace"); str.reverse(); System.out.println(str);
Output : ecaRavaJ
This method replaces the string from specified start index to the end index.
StringBuffer str = new StringBuffer("Hello World"); str.replace( 6, 11, "Java"); System.out.println(str);
Output : Hello java
This method returns the current capacity of StringBuffer object.
StringBuffer str = new StringBuffer(); System.out.println( str.capacity() );
Output : 16
Note: Empty constructor reserves space for 16 characters. Therefore the output is 16.
This method is used to ensure minimum capacity of StringBuffer object.
If the argument of the ensureCapacity() method is less than the existing capacity, then there will be no change in existing capacity.
If the argument of the ensureCapacity() method is greater than the existing capacity, then there will be change in the current capacity using following rule: newCapacity = (currentCapacity*2) + 2.
StringBuffer str = new StringBuffer(); System.out.println( str.capicity()); //Output: 16 str.ensureCapacity(30); //greater than the existing capacity System.out.println( str.capacity()); //Output: 34 (currentcapacity*2) + 2.) i.e(16*2)+2 = 34.
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc