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";
System.out.println(s);
/*
StringBuffer s = new StringBuffer("just a string") ; //StringBuilder s = new StringBuilder("just a string");
s = s.append("add me too");
System.out.println(s);
*/
}
}
Alright, now have a look on the bytecode of this program(don't be panic).
>> javac StringTest.java
>> javap -c StringTest
Compiled from "StringTest.java"
class StringTest extends java.lang.Object{
StringTest();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object.
4: return
public static void main(java.lang.String[]);
Code:
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/
String;)Ljava/lang/StringBuilder;
14: ldc #6; //String add me too
16: invokevirtual #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)
Ljava/lang/StringBuilder;
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 StringTest.java
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
6 comments:
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??
Hey thanks Nehil,
Internals of bytecode, you just run javap command, rest jdk will take care of
Wow, it was great. You can see the some more information from the following link.
http://www.computersight.com/Programming/Java/Compare-String-Stringbuffer-and-Stringbuilder.141705
hai vaibhav..
this is sravan
i had little bit confusion earlier regarding String and StringBuffer/StringBuilder
thax for details explaination..
Welcome Sravan
Good analysis
Thanks a lot
Post a Comment