Advertisement



< Prev
Next >



StringBuffer Class





By using the String class, we can make unmodifiable strings, i.e. immutable strings, but by using the StringBuffer class we can not only create strings of characters but can also modify them, which makes them mutable strings. The StringBuffer class is also a thread-safe class because its methods are marked synchronized.




When StringBuffer class is used?







Constructors of StringBuffer class







Creating an empty StringBuffer object



// Java - StringBuffer Class Example


class StringBufferEx
{
public static void main(String... ar)
{
	//Creating an empty StringBuffer object.
	StringBuffer sb= new StringBuffer(); 
	
	//Printing the value of StringBuffer object
	System.out.println("Original StringBuffer value : "+ sb);
	
	//Appending a String to the StringBuffer object
	sb.append("Stay");
	
	//Appending another String to the StringBuffer object
	sb.append("Positive");
	
	//Printing the modified value of StringBuffer object
	System.out.println("Modified StringBuffer value : "+ sb);
}
}


Output is :


Original StringBuffer value is : 
Modified StringBuffer value is : StayPositive


Program Analysis





Advertisement




Creating and initializing a StringBuffer object using a String



// Java - StringBuffer Class Example

class StringBufferEx
{
public static void main(String... ar)
{
	//Creating and initializing a StringBuffer object using a String value
	StringBuffer sb= new StringBuffer("Keep");

	//Printing the value of StringBuffer object
	System.out.println("Original StringBuffer value : "+ sb);
	
	//Appending a String to the StringBuffer object
	sb.append("Smiling");
	
	//Printing the modified value of the StringBuffer object
	System.out.println("Modified StringBuffer value : "+ sb);
}
}


Output is :


Original StringBuffer value : Keep
Modified StringBuffer value : KeepSmiling


Program Analysis







Finding the index of a substring in a string created by StringBuffer


In the next example, we will be finding the index of a substring within in a string created by the StringBuffer class by calling its indexOf method.

// Java - Example of StringBuffer class

class A
{
public static void main(String... ar()
{
	//Creating a StringBuffer object
	StringBuffer sb= new StringBuffer("Blue"); 
	
	//Printing the StringBuffer object
	System.out.println("Original StringBuffer value : "+ sb); 
	
	//Finding the index of substring "ue" in StringBuffer string
	int index = sb.indexOf("ue"); 
	
	//Printing the index position of a substring "ue" in StringBuffer string 
	System.out.println("index of ue  in StringBuffer string  : "+  index);
}


Output is :


Original StringBuffer string value : Blue
index of ue in StringBuffer string : 2




Please share this article -




< Prev
Next >
< substring() method
StringBuffer methods >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement