Advertisement



< Prev
Next >



StringBuffer v/s StringBuilder





Although both StringBuilder class and StringBuffer class are used to create a string of characters which are modifiable, there are a few differences between them. Let's discuss those.




Differences between StringBuffer class and StringBuilder class.


StringBuffer StringBuilder

StringBuffer class is slower to execute than StringBuilder.


StringBuilder class is faster to execute than StringBuffer Class.


StringBuffer class methods are marked with synchronized keyword.


StringBuilder class methods are not marked with synchronized keyword.


StringBuffer class is a thread-safe class.


StringBuilder class is not a thread-safe class.


StringBuilder class is a good candidate for creating applications where multiple threads are going to perform string modifications.


StringBuilder class is a good candidate for creating applications where only one thread is performing string modifications.


StringBuffer class is not a good candidate for creating applications that require a fast modification of strings.


StringBuilder class is a good candidate for creating applications that require a fast modification of strings.






Creating and initializing a StringBuffer object using a String



// Java - Example of StringBuffer class

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





Advertisement




Creating and initializing a StringBuilder object using a String


// Java - Example of StringBuilder class

class StringBuilderEx
{
public static void main(String... ar)
{
	//Creating a StringBuilder object
	StringBuilder sb= new StringBuilder("Happy");

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


Output is :


Original StringBuilder value : Happy
Modified StringBuilder value : HappyFaces


Program Analysis







Execution speed of StringBuffer & StringBuilder


In the next example, we are going to prove that the execution speed of StringBuffer class is slower than StringBuilder. For this, we will perform the append operation by calling the append() method of each class, 50000 times.

// Java - Difference between the execution speed of StringBuffer & StringBuilder class


class BuffBuild 
{
public static void main(String[] args) 
{
	int counter = 50000;

	//Creating an empty StringBuffer object
	StringBuffer sb1 = new StringBuffer();
	
	//Getting the current starting time 
	long startTime = System.currentTimeMillis();    	


	//Using for loop to call the append() method of StringBuffer, 5000 times
	for (int i = 0; i<counter; i++) 
	{
		sb1.append("StringBuffer");
	}
	
	//Getting the time taken by StringBuffer
	long timeTaken = System.currentTimeMillis()-startTime();
	
	//Printing the time taken by StringBuffer to call the append() method, 5000 times
	System.out.println("Milliseconds taken by StringBuffer  to append() 50000 times : " + timeTaken);
        
		
	//Creating an empty StringBuffer object  
	StringBuilder sb2 = new StringBuilder();
	
	//Getting the current starting time
	startTime = System.currentTimeMillis(); 		

	//Using for loop to call the append() method of StringBuffer, 5000 times
	for (int i = 0; i<counter; i++)
	{
		sb2.append("StringBuilder");
	}
	
	//Calculating the time taken by StringBuilder
	timeTaken = System.currentTimeMillis()-startTime();
    
	//Printing the time taken by StringBuilder to call the append() method, 5000 times
	System.out.println("Milliseconds taken by StringBuilder to append() 50000 times : " + timeTaken);
        
}
}


Output is :


Milliseconds taken by StringBuffer  class to append() 50000 times : 21
Milliseconds taken by StringBuilder class to append() 50000 times : 15


Program Analysis





Please share this article -




< Prev
Next >
< StringBuilder methods
Byte Streams in Java >



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