Difference between String and String Buffer /StringBuilder in Java – 3 Important Points To Know

Share and Enjoy !

Introduce to Difference between String and String Buffer

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 get the output.

It means when you do some alternation to the string it is going to create a 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 the 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.

READ  Class GZIPOutputStream in Java

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 the StringBuilder class then?

Well, the StringBuffer class is thread-safe basically synchronized. So when you are going for a single-thread application StringBuilder comes in handy. It is not threaded safe i.e synchronized. So where the single thread will run, the StringBuilder class will give a 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’.

Read more: class-throwable-in-java

No, you can compare which one is better.

Share and Enjoy !

8 thoughts on “Difference between String and String Buffer /StringBuilder in Java – 3 Important Points To Know

  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.

  4. Hmm it seems like your blog ate my first comment (it was extremely long) so I guess I’ll just sum it up what I submitted and say, I’m thoroughly enjoying your blog.
    I too am an aspiring blog blogger but I’m still new to the
    whole thing. Do you have any tips for beginner blog writers?
    I’d definitely appreciate it.

Leave a Reply

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