Ad Home

Theme images by kelvinjay. Powered by Blogger.

Travel

Header Ads

Java

Selenium

UFT

Framework

General QA Concept

BDD

» » Difference between String and StringBuffer/StringBuilder in Java


Difference between String and String Buffer/String Builder in Java

String class is an important class in java.From beginner to expert all of us use, string and system.out.print() method to debug and getting output.

But String is not normal class(Read more on http://www.askqtp.com/2012/01/why-string-class-in-not-like-other.html). It is immutable and final class.What is that??

It means when you do some alternation to the string it is going to create new string Object and the pointer changes the new string Object.So, if you were to do some string operation involving an append or trim or some other method call to modify your string object, you would really be creating those many new objects of class String.

Well what is the impact?? Check out this:
I have used the below code for String:
import java.text.SimpleDateFormat;

import java.util.Date;

public class Calculator {
public Calculator() {
super();
}
public static void main(String Args[]) {
String s1 = "Hi";
String s2 = "Bye";
System.out.println("start time->"+getCurrentTimeStamp());// this is for tracking the time
for (int i = 0; i < 100000; i++)
{
 s1 += s2;
 }
 System.out.println("end time"+getCurrentTimeStamp());
}
 public static String getCurrentTimeStamp()
{
 SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//dd/MM/yyyy
 Date now = new Date();
 String strDate = sdfDate.format(now);
 return strDate;
 }
}

Before start of the operation:





During the execution:

So using string is operation is no doubt, will create a performance issue(as you can see the CPU usage went high in my case while execution.)
To overcome this issue Java has introduced String buffer class. The same set of operation can be done using String Buffer.This is a mutable class so string operation will be fast and efficient.

Lets test on StringBuffer class:

I have used the below code for StringBuffer:

public static void main(String Args[])
{
StringBuffer sBuffer = new StringBuffer(" Hello");
sBuffer.append(" String Buffer");
System.out.println(sBuffer);
for (int i = 0; i < 100000; i++)
{
 sBuffer.append("world");
 }
 System.out.println("end time"+getCurrentTimeStamp());
}
What is the impact now on performance? Lets check--
Before Execution:




During Execution:





Now what is String Builder class then?
Well StringBuffer class is thread safe basically synchronized.So when you are going for single thread application StringBuilder comes handy. It is not thread safe i.e synchronized. So where the single thread will run ,StringBuilder class will give more efficient way to work with String.


I have used the below code for StringBuilder:
public static void main(String Args[])
{
StringBuilder sb = new StringBuilder("Hello");
String s1 = "Hi";
String s2 = "Bye";
System.out.println("start time->"+getCurrentTimeStamp());
for (int i = 0; i < 100000; i++)
 {
 sb.append("world");
 }
 System.out.println("end time"+getCurrentTimeStamp());


Before Execution:

So after and during running the code...



So what is the bottom line:


  • If your string is not going to change use a string Class because a String object is immutable.
  • If your string can change (lots of logic and operations in the construction of the string) and will only be accessed from a single thread, use a StringBuilder is good enough. 
  • If your string can change, and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous so you have thread-safety.But according to stack overflow  StringBuffer is largely obsolete and should be replaced by the non-synchronized implementation 'StringBuilder'.

No you can compare which one is better.


«
Next
Newer Post
»
Previous
Older Post

3 comments:

  1. This is good..Nice Article

    ReplyDelete
  2. String is an immutable class while StringBuilder & StringBuffer are mutable classes. StringBuffer is synchronized while StringBuilder is not.

    Below link can be useful to find more differences between String, StringBuffer & StringBuilder

    Difference between String, StringBuffer and StringBuilder

    http://newtechnobuzzz.blogspot.com/2014/07/difference-between-string-stringbuffer.html

    ReplyDelete