Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!nx01.iad01.newshosting.com!newshosting.com!69.16.185.11.MISMATCH!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!postnews.google.com!j14g2000prn.googlegroups.com!not-for-mail From: lewbloch Newsgroups: comp.lang.java.programmer Subject: Re: StringBuilder Difficulties Date: Mon, 4 Jul 2011 04:07:39 -0700 (PDT) Organization: http://groups.google.com Lines: 49 Message-ID: References: <971fj1Fg1rU1@mid.individual.net> <7c5d329e-6db5-4b52-8501-b9ae9c3a8738@k23g2000pri.googlegroups.com> <97auvhFt73U1@mid.individual.net> NNTP-Posting-Host: 108.89.33.208 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1309777660 31136 127.0.0.1 (4 Jul 2011 11:07:40 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 4 Jul 2011 11:07:40 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: j14g2000prn.googlegroups.com; posting-host=108.89.33.208; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-Header-Order: HNKRUAELSC X-HTTP-UserAgent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30,gzip(gfe) Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:5846 On Jul 3, 3:35=A0am, Robert Klemme wrote: > On 03.07.2011 09:08, lewbloch wrote: > > > The efficiency doesn't come from the re-use of the 'StringBuilder' > > object from iteration to iteration. =A0The create/destroy mechanism for > > short-lived objects favors creating the temporary object inside the > > loop, not outside. =A0The efficiency comes from not having to create so > > many superfluous 'String' objects. > > Lew, I find that explanation a bit strange: only reuse of StringBuilder > makes it possible to not have to create all those superfluous String > instances. =A0You make it sound as if reuse and not having to create > superfluous String objects are somehow antithetic or independent. =A0If I > cannot use a StringBuilder and I want to construct a String from parts I > have to create superfluous String instances - so both facts are really > related. > They are independent, and I deny that I made them sound antithetic. That's on your inference, not my implication. The reuse of the 'StringBuilder' is not needed in some loops, depending on whether the building occurs inside the loop entirely or not. That's independent of the notion that 'StringBuilder' saves on intermediate 'String' objects. You can tell I didn't imply that the two are antithetical because I explicitly said to put the 'StringBuilder' inside the loop if that's what the logic requires. If you're paying attention you can tell, at least. Consider: while ( someCondition ) { StringBuilder sb =3D new StringBuilder(); sb.append( getInitialStuff() ); Result result =3D doSomething(); sb.append( ". Result: " ).append( result.toString() ); Sumpn moreShite =3D doSomethingWith( result ); sb.append( ". Moreover: " ).append( moreShite.toString() ); output( sb ); } Nothing in the logic of this example requires 'sb' to have scope outside the loop, so it's foolish to put it outside the loop. This is independent of, and entirely compatible with, the benefits of using 'StringBuilder' to avoid a lot of intermediate 'String' objects. -- Lew