My comment is about using plus out side of loops. Obviously we should use StringBuilder when using loops. But we shouldn't uses it unnecessary outside of loops since it affects readability.
On Thu, Dec 17, 2015 at 11:23 AM, Rasika Perera <rasi...@wso2.com> wrote: > Hi All, > > Please find comments inline. > > Yes, this has been the case for sometime where internally append is used. >> Try the same with a loop. > > +1 have tried this. Even though compiler is optimized to put StringBuilder > on multiple "+" concats not too *smart* to use the same StringBuilder > instance inside a loop. > > Tried Test.java with a string concat inside a *loop*. > > *Test.java* > class Test { > public static void main(String[] args) { > String content = ""; > for (int i = 0; i < 10; i++) { > content = content + String.valueOf(i); > } > System.out.println(content); > } > } > > *Constant pool:* > const #3 = class #24; // java/lang/StringBuilder > > *Main Method:* > public static void main(java.lang.String[]); > Code: > 0: ldc #2; //String > 2: astore_1 > 3: iconst_0 > 4: istore_2 > 5: iload_2 > 6: bipush 10 > 8: if_icmpge 39 > 11: new #3; //class java/lang/StringBuilder <-------*creating new > String builder each cycle* > 14: dup > 15: invokespecial #4; //Method java/lang/StringBuilder."<init>":()V > 18: aload_1 > 19: invokevirtual #5; //Method > java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; > 22: iload_2 > 23: invokestatic #6; //Method > java/lang/String.valueOf:(I)Ljava/lang/String; > 26: invokevirtual #5; //Method > java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; > 29: invokevirtual #7; //Method > java/lang/StringBuilder.toString:()Ljava/lang/String; > 32: astore_1 > 33: iinc 2, 1 > 36: goto 5 <--------------- *Jump for the loop* > 39: getstatic #8; //Field java/lang/System.out:Ljava/io/PrintStream; > 42: aload_1 > 43: invokevirtual #9; //Method > java/io/PrintStream.println:(Ljava/lang/String;)V > 46: return > } > > Thanks, > Rasika > > On Wed, Dec 16, 2015 at 6:19 AM, Afkham Azeez <az...@wso2.com> wrote: > >> Yes, this has been the case for sometime where internally append is used. >> Try the same with a loop. >> >> On Tue, Dec 15, 2015 at 11:43 PM, Manuranga Perera <m...@wso2.com> wrote: >> >>> I have compiled following class using javac 1.6.0_38 >>> >>> class X{ >>> public String m(String a, String b, String c){ >>> return a + b + c; >>> } >>> } >>> >>> >>> and decoupled using javap >>> >>> >>> class X { >>> X(); >>> Code: >>> 0: aload_0 >>> 1: invokespecial #1 // Method >>> java/lang/Object."<init>":()V >>> 4: return >>> >>> public java.lang.String m(java.lang.String, java.lang.String, >>> java.lang.String); >>> Code: >>> 0: new #2 // class >>> java/lang/StringBuilder >>> 3: dup >>> 4: invokespecial #3 // Method >>> java/lang/StringBuilder."<init>":()V >>> 7: aload_1 >>> 8: invokevirtual #4 // Method >>> java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; >>> 11: aload_2 >>> 12: invokevirtual #4 // Method >>> java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; >>> 15: aload_3 >>> 16: invokevirtual #4 // Method >>> java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; >>> 19: invokevirtual #5 // Method >>> java/lang/StringBuilder.toString:()Ljava/lang/String; >>> 22: areturn >>> } >>> >>> As you can see, there are three appends but only one StringBuilder >>> objects. Therefor I purpose using plus instead of append in our code. >>> >>> >>> >>> -- >>> With regards, >>> *Manu*ranga Perera. >>> >>> phone : 071 7 70 20 50 >>> mail : m...@wso2.com >>> >> >> >> >> -- >> *Afkham Azeez* >> Director of Architecture; WSO2, Inc.; http://wso2.com >> Member; Apache Software Foundation; http://www.apache.org/ >> * <http://www.apache.org/>* >> *email: **az...@wso2.com* <az...@wso2.com> >> * cell: +94 77 3320919 <%2B94%2077%203320919>blog: * >> *http://blog.afkham.org* <http://blog.afkham.org> >> *twitter: **http://twitter.com/afkham_azeez* >> <http://twitter.com/afkham_azeez> >> *linked-in: **http://lk.linkedin.com/in/afkhamazeez >> <http://lk.linkedin.com/in/afkhamazeez>* >> >> *Lean . Enterprise . Middleware* >> >> _______________________________________________ >> Dev mailing list >> Dev@wso2.org >> http://wso2.org/cgi-bin/mailman/listinfo/dev >> >> > > > -- > With Regards, > > *Rasika Perera* > Software Engineer > M: +94 71 680 9060 E: rasi...@wso2.com > LinkedIn: http://lk.linkedin.com/in/rasika90 > > WSO2 Inc. www.wso2.com > lean.enterprise.middleware > -- With regards, *Manu*ranga Perera. phone : 071 7 70 20 50 mail : m...@wso2.com
_______________________________________________ Dev mailing list Dev@wso2.org http://wso2.org/cgi-bin/mailman/listinfo/dev