Sunday, March 25, 2007

String vs StringBuffer vs StringBuilder

Let me first tell you what is StringBuilder. StringBuilder is a class analogous to StringBuffer added in JDK 1.5. This class is designed to use in place where StringBuffer is used by single thread(like in most of the cases). According to documentation, StringBuilder should work faster than StringBuffer. So " thread unsafe, fast".

I was reading one of the posts of orkut Java community asking "what is this capacity in StringBuffer and even we can add two strings from String class why to go for StringBuffer". Valid question ! GC need to work little more in case of String, but thats fair. As a java wellwisher let me try to do some publicity of Java API :D. And here it goes:

No don't use String class for concatenation operation, always use StringBuffer/StringBuilder and let me tell you why ?

This is a simple Java code for string addition in String and StringBuffer:

class StringTest {
public static void main(String[] args)
String s = "just a string";
s = s + "add me too";
StringBuffer s = new StringBuffer("just a string") ; //StringBuilder s = new StringBuilder("just a string");
s = s.append("add me too");

Alright, now have a look on the bytecode of this program(don't be panic).

>> javac
>> javap -c StringTest

Compiled from ""
class StringTest extends java.lang.Object{
0: aload_0
1: invokespecial #1; //Method java/lang/Object.
4: return

public static void main(java.lang.String[]);
0: ldc #2; //String just a string
2: astore_1
3: new #3; //class java/lang/StringBuilder
6: dup
7: invokespecial #4; //Method java/lang/StringBuilder.
10: aload_1
11: invokevirtual #5; //Method java/lang/StringBuilder.append:(Ljava/lang/
14: ldc #6; //String add me too
16: invokevirtual #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)
19: invokevirtual #7; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
22: astore_1
23: getstatic #8; //Field java/lang/System.out:Ljava/io/PrintStream;
26: aload_1
27: invokevirtual #9; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
30: return


Just see line no. 11. Interesting, the plus sign we used for addition is not as innocent as it looks. String itself use StringBuffer(StringBuilder) to add two strings and hence taking much more time than normal append operation done by StringBuffer. Let me give you more evidence, run verbose option and check the time

>> javac -verbose

and check the other one, that is, with StringBuffer one.

You can clearly figure out the time difference and make a try with StringBuilder, time should reduce furthermore.

So, how is my publicity :D


nehil said...

Wow Sir!! i never thought so deep. but it was quite helpful and interesting.
by the way how u get to know the internals of bytecode??

Vaibhav said...

Hey thanks Nehil,

Internals of bytecode, you just run javap command, rest jdk will take care of

Anonymous said...

Wow, it was great. You can see the some more information from the following link.

sravan kumar said...

hai vaibhav..
this is sravan

i had little bit confusion earlier regarding String and StringBuffer/StringBuilder

thax for details explaination..

Vaibhav said...

Welcome Sravan

Vinayak said...

Good analysis
Thanks a lot