Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Gene Wirchenko Newsgroups: comp.lang.java.programmer Subject: Re: StringBuilder Difficulties Date: Wed, 29 Jun 2011 18:55:14 -0700 Organization: A noiseless patient Spider Lines: 67 Message-ID: References: <971fj1Fg1rU1@mid.individual.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Injection-Info: mx04.eternal-september.org; posting-host="7Qrvczazr82YckO5XW8Vtw"; logging-data="5096"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18pdXsIxoANVCci/2ZUyrOX+ewMhbv9kQE=" X-Newsreader: Forte Agent 4.2/32.1118 Cancel-Lock: sha1:SHFVZ3vAaMb0SdSeDNZCiENOfi4= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:5790 On 29 Jun 2011 20:17:37 GMT, blmblm@myrealbox.com wrote: >In article , >Gene Wirchenko wrote: [snip] >> Am I missing something about StringBuilder, or is it really this >> difficult to play with? It would make a lot more sense to me if >> StringBuilder worked more like String does. [snip] >up a string piece by piece. You *could* write something like > >String a = "first"; >a += " second"; >a += " etc"; > >and my *guess* is that this is not horribly inefficient if the >number of concatenation operations is small. (The conventional IOW, who cares if it is only a bit of inefficiency? Agreed. >wisdom, as I understand it, is that the Java runtime is pretty >good at managing short-lived objects, so creating new objects is >not invariably something to avoid, though as with anything else >one shouldn't get carried away, maybe. Again the experts may >disagree.) > >But if there are a lot of concatenation operations it's said to be >more efficient to use a StringBuilder, e.g.: > >StringBuilder sb = new StringBuilder(); >sb.append("first"); >sb.append(" second"); >sb.append(" etc"); >String a = sb.toString(); It is. My test code appends one character at a time. Switching from String to StringBuilder cut the execution time by about 40%. >The above is what I mostly use StringBuilder for; there are >undoubtedly other things one can do with it as well, some of >which may be useful to you (I haven't followed carefully all the >threads you've started). I have managed to get to the point where I have found out what you have posted here, and I agree with it, too. >(I'm not optimistic that this commentary will be helpful, or >even that it will be read [*], given that you didn't reply to >my posts in the threads about passing method names to methods, >but I guess I'll try again .... ) > >[*] That's not actually meant as snark; not long ago I changed >the e-mail address I use to post, to a GMail one, and I'm under >the impression that some Usenet participants routinely filter out >anything from a GMail address, so I worry just a bit .... Not me. I killfile only the deserving (meaning those undeserving of my time). Sincerely, Gene Wirchenko