< 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


// StringBuffer Class Example

class StringBufferEx
{
public static void main(String... ar)
{
StringBuffer sb= new StringBuffer("Keep");

System.out.println("Original StringBuffer value : "+ sb);
sb.append("Smiling");
System.out.println("Modified StringBuffer value : "+ sb);
}
}


Output is :


Original StringBuffer value : Keep
Modified StringBuffer value : KeepSmiling


Program Analysis







Creating and initializing a StringBuilder object using a String


// StringBuilder Class Example

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

System.out.println("Original StringBuilder value : "+ sb);
sb.append("Faces");
System.out.println("Modified StringBuilder value : "+ sb);
}
}


Output is :


Original StringBuilder value : Happy
Modified StringBuilder value : HappyFaces


Program Analysis







Execution speed of StringBuffer & StringBuilder

We are going to prove that the execution speed of StringBuffer class is slower than StringBuilder when performing the append() method of each class, 50000 times.
class BuffBuild 
{
public static void main(String[] args) 
{
int counter = 50000;

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

for (int i = 0; i<counter; i++) 
{
	sb1.append("StringBuffer");
}
long timeTaken = System.currentTimeMillis()-startTime();//Getting the time taken by StringBuffer
System.out.println("Milliseconds taken by StringBuffer  to append() 50000 times : " + timeTaken);
        

        
StringBuilder sb2 = new StringBuilder();
startTime = System.currentTimeMillis(); 		//Getting the current starting time

for (int i = 0; i<counter; i++)
{
	sb2.append("StringBuilder");
}
timeTaken = System.currentTimeMillis()-startTime();    //Getting the time taken by StringBuilder
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 -

Facebook Google Pinterest Reddit Tumblr Twitter



< Prev
Next >
< StringBuilder methods
Byte Streams in Java >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2