Advertisement
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. |
// 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);
}
}
Original StringBuffer value : Keep
Modified StringBuffer value : KeepSmiling
Advertisement
// 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);
}
}
Original StringBuilder value : Happy
Modified StringBuilder value : HappyFaces
// 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);
}
}
Milliseconds taken by StringBuffer class to append() 50000 times : 21
Milliseconds taken by StringBuilder class to append() 50000 times : 15
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement