Difference between StringBuffer and StringBuilder

StringBuffer and StringBuilder:

The string is immutable class which created the new String object whenever the string is concat or append. The StringBuffer and StringBuilder are mutable class where it will not create the new String object at the time of append / concat instead it just modify the String in same object. Even though StringBuffer and StringBuilder terminology are same, but there is some difference in it which we are going to discuss now.
stringbuffer,stringbuilder,stringbuffer vs stringbuilder,java stringbuffer,stringbuffer stringbuilder,stringbuilder and stringbuffer,string and stringbuffer,stringbuilder java,stringbuffer and stringbuilder,java stringbuffer and stringbuilder,string stringbuffer and stringbuilder difference,differnce between stringbuffer and stringbuilder,diffrence between stringbuffer and stringbuilder,java stringbuilder,string,string vs stringbuilder


StringBuffer:

In StringBuffer every methods are synchronized which means only one thread is allowed to append the string with the cost of performance. It provides thread safety which allows only one thread to perform whereas the other thread has to wait for it's time.
java.lang.StringBuffer extends java.lang.AbstractStringBuilder implements java.io.Serializable, java.lang.CharSequence

Constructor:

  • StringBuffer() - It will create the empty buffer with initial capacity of 16
  • StringBuffer(String s) - it will create buffer with specified string 
  • StringBuffer(int capacity) - it will create the empty buffer with specified capacity


Example:

public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java ");
sb.append("program");
System.out.println(sb);
}
}

Output:
Java program


StringBuilder:

It is introduced in 1.5 version. In StringBuilder every methods are non-synchronized which means multiple threads are allowed to append the string at the same time. It is not thread safe. It relative improve the performance compare to the StringBuilder.
java.lang.StringBuilder extends java.lang.AbstractStringBuilder implements java.io.Serializable, java.lang.CharSequence

Constructor:

  • StringBuilder() - It will create the empty builder in a memory with initial capacity of 16
  • StringBuilder(String s) - it will create builder with specified string 
  • StringBuilder(int capacity) - it will create the empty builder with specified capacity

Example:

public class Example
{
public static void main(String[] args)
{
StringBuilder sb = new StringBuilder("Java ");
sb.append("program");
System.out.println(sb);
}
}

Output:
Java program


Performance Example:

class StringPerformance
{
public void getStringBufferPerformance()
{
long start = System.currentTimeMillis();
StringBuffer sbuff = new StringBuffer();
for(int i=0;i<=10000;i++)
{
sbuff.append(i);
}
System.out.println("StringBuffer ended in " + (System.currentTimeMillis()-start) + " ms");
}

public void getStringBuilderPerformance()
{
long start = System.currentTimeMillis();
StringBuilder sbuild = new StringBuilder();
for(int i=0;i<=10000;i++)
{
sbuild.append(i);
}
System.out.println("StringBuilder ended in " + (System.currentTimeMillis()-start) + " ms");
}
}
public class Example
{
public static void main(String[] args)
{
StringPerformance obj = new StringPerformance();
obj.getStringBufferPerformance();
obj.getStringBuilderPerformance();
}
}

Output:
StringBuffer ended in 16 ms
StringBuilder ended in 0 ms


Comments

Popular posts from this blog

Multiple image upload to database and display from it

PHP Login and Registration script

String methods in java