Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: blmblm@myrealbox.com Newsgroups: comp.lang.java.programmer Subject: Re: StringBuilder Difficulties Date: 30 Jun 2011 20:30:00 GMT Organization: None Lines: 77 Message-ID: <9744m8FpnhU3@mid.individual.net> References: <971fj1Fg1rU1@mid.individual.net> X-Trace: individual.net LG0ERYh1+HCD3WebhIzXwAxP6wYYxcYsN6C/OSndrVlTqTh00S X-Orig-Path: not-for-mail Cancel-Lock: sha1:28xh3x10vdWvGc5+ugjxWPaeOSA= X-Newsreader: trn 4.0-test76 (Apr 2, 2001) Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:5795 In article , Gene Wirchenko wrote: > wrote: > > >In article , > >Gene Wirchenko wrote: [ snip ] > >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%. So okay, you probably *do* want to use StringBuilder, and having that confirmed by experiment is good. [ snip ] > >(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). More useful information; thanks for the reply. A general comment: I'm inclined to agree with the people who are saying that in general it seems like you're trying to write [name of your favorite language] programs in Java, and in the long term that seems less optimal than trying to grok the Java mindset. I think part of it may be struggling with the object-oriented paradigm, but part of it may just be coming to terms with the fact that Java is, as I think Patricia Shanahan said not long ago (possibly in another thread), that Java is just plain verbose. But I have some sympathy with the desire just to get something running: I spent a number of hours a while back trying to teach myself some Scheme and in the process trying make it conform to my strongly-typed-languages-trained mindset, and I'd probably have done better to get a good introductory book and try to grok the no-types(?) mindset. (Maybe I'll try again at some point.) -- B. L. Massingill ObDisclaimer: I don't speak for my employers; they return the favor.