Difference between String and StringBuffer/StringBuilder in Java

Share and Enjoy !


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 https://www.techtravelhub.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:

Ads code goes here






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.

 

 

Share and Enjoy !

7 thoughts on “Difference between String and StringBuffer/StringBuilder in Java”

  1. Great post. I was checking constantly this blog and
    I am impressed! Very useful information particularly the last part :
    ) I care for such information much. I was seeking this
    certain information for a long time. Thank you and good luck.
    adreamoftrains best hosting

  2. Greetings! Very helpful advice in this particular post! It’s
    the little changes that produce the most important changes.
    Thanks for sharing!

  3. I have read a few excellent stuff here. Definitely worth bookmarking for revisiting.
    I surprise how much effort you put to create this sort of magnificent informative web site.

Leave a Comment

Your email address will not be published. Required fields are marked *