Stringbuffer 2005-10-23 - By Frans Verhoef
Back Hi,
You're actually asking the wrong question. The question is more how many StringBuffer objects are created. In method 1, 3 StringBuffer objects are created. One is the StringBuffer you created yourself, but also each time you use the + to concatenate two string, a StringBuffer is created by the JVM. Therefore, in method 2, only one StringBuffer is created, as you append all strings directly to your already existing StringBuffer, hence no extra StringBuffers are need.
The amount of Strings created are the same, as in both cases only 4 strings are generated ("beta", "The strings are ", "a", and "c").
Cheers, Frans
On Sun, 23 Oct 2005 17:39:19 -0600, SUBSCRIBE EJB-INTEREST anonymous wrote: >?Hi, >?Could anyone please mail be which one would be better of the below >?two in performance. >?How many strings will be created in Method1 and how many in >?Method2? Thanks in advance. > >?Method - 1 > >?String b = "beta"; >?StringBuffer buf = new StringBuffer("The strings are "); >?buf.append("a"+ b + "c"); > >?Method - 2 >?String b = "beta"; >?StringBuffer buf = new StringBuffer("The strings are "); >?buf.append("a").append(b).append("c"); > >?=====================================================================>?===== To unsubscribe, send email to listserv@(protected) and >?include in the body of the message "signoff J2EE-INTEREST". ?For >?general help, send email to listserv@(protected) and include in >?the body of the message "help".
==========================================================================To unsubscribe, send email to listserv@(protected) and include in the body of the message "signoff J2EE-INTEREST". For general help, send email to listserv@(protected) and include in the body of the message "help".
|
|