It's been 3 years when I have not done any blogging here. Some day before, one of my friends was asking me about StringBuffer and he has the point that I don't have any justification that why Sun has created StringBuffer.
I am writing this blog from a very rural village of Bihar, India. The common problem I found was people are not utilizing the time in best of work. Many of the kids go to the market to bring one-one item at a time. Alright, are we engineers also follow the same trend.
We use String as default and then we keep adding things in it. Something like:
String dontUse = "This";
dontUse +="is not right";
Alright, here is a small code I have written to understand the estimated time.
public class StringBufferExample {
public static void main(String[] args) {
String[] dontUse = new String[10000];
//StringBuffer[] dontUse = new StringBuffer[10000];
for(int i=0;i<10000;i++) { }
long startTime = System.nanoTime();
for(int i=0;i<10000;i++) {
dontUse[i]= new String("this");
// dontUse[i]= new StringBuffer("this");
}
for(int i=0;i<10000;i++) {
dontUse[i]+="is wrong";
// dontUse[i].append("is wrong");
}
long endTime = System.nanoTime();
System.out.println(endTime - startTime);
}
}
Approx Time taken from this code:
5501435(ns) whereas if you run the commented code, it will take:
2258812(ns)
So, not visible but normal String operation for "simply" addition of two string is "twice" costlier than StringBuffer.
Running: javap -c -classpath . StringBufferExample (copying those lines which are costly), will clearly tell you why String operation is a costly affair(actually it was never a String operation, it changes things to StringBuffer and then again convert it by toString to String).
64: if_icmpge 97
67: new #6; //class java/lang/StringBuilder
70: dup
71: invokespecial #7; //Method java/lang/StringBuilder."<init>":()V
74: aload_1
75: iload 4
77: dup2_x1
78: aaload
79: invokevirtual #8; //Method java/lang/StringBuilder.append:(Ljava/lang/
String;)Ljava/lang/StringBuilder;
82: ldc #9; //String is wrong
84: invokevirtual #8; //Method java/lang/StringBuilder.append:(Ljava/lang/
String;)Ljava/lang/StringBuilder;
87: invokevirtual #10; //Method java/lang/StringBuilder.toString:()Ljava/l
ang/String;
90: aastore
Now in the next blog, I will cover how StringBuffer handles the capacity, how it enlarge its capacity and when. It's a pretty simple code written in JDK.