Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

Java StringBuffer Class

public final class StringBuffer
extends Object
implements Serializable, CharSequence

  • • Like String class, StringBuffer is another class specifically used to represent and manipulate strings and which can be modified.
  • • That means at any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.
  • StringBuffer class is thread-safe i.e. multiple threads cannot access it simultaneously, So it is safe and will result in an order.
  • StringBuffer defines following constructors,

    1.StringBuffer():creates an empty string buffer with the initial capacity of 16.
    2.StringBuffer(String str): creates a string buffer with the specified string.
    3.StringBuffer(int capacity): creates an empty string buffer with the specified capacity as length.

Example:

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
  }
}

Reason:

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.


append()

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);

insert():

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


reverse():

This method reverses the characters within a StringBuffer object.

StringBuffer str = new StringBuffer("JavaRace");

str.reverse();

System.out.println(str);

Output : ecaRavaJ


replace():

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


capacity():

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.


ensureCapacity():

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.

About the Author



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






 PreviousNext