Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!feeder.erje.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 12:11:55 -0700 Organization: A noiseless patient Spider Lines: 86 Message-ID: <5otm07t5lrvp0me457o7f3s2lpe7uo5mk6@4ax.com> References: 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="11694"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/g4oe8WuEKbcjtnpsnijp30XnGmkd+Duw=" X-Newsreader: Forte Agent 4.2/32.1118 Cancel-Lock: sha1:vaMRAAUEYVXH4JwALkYyHruTr6Q= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:5779 On Tue, 28 Jun 2011 21:29:00 -0400, Eric Sosman wrote: >On 6/28/2011 8:54 PM, Gene Wirchenko wrote: >> Dear Java'ers: >> >> I am working with StringBuilder now. I grant that it is faster >> in execution, but it is taking a bunch of my time to get it straight. >> I decided to change my VRString class (call-by-value-result) to >> VRStringB. Complications ensued. >> >> The amount of ornamentation required in my code was nasty, so I >> did some simplifying. >> >> How does one assign a String value to a StringBuilder variable? ^^^^^ > One does not "assign" a reference of type T1 to a reference ^^^^^^^^^ I wrote "value". >of unrelated type T2. One can, however, create a StringBuilder >whose initial content is the same as that of a given String: > > String str = ...; // non-null > StringBuilder sbd = new StringBuilder(str); I have found that, but I was wondering about how to do it with the same StringBuilder object. I thought that part of the advantage of using StringBuilder was that the amount of object creation got cut down. >These are "the same" in the sense that str.length() == sbc.length() >and str.charAt(k) == sbd.charAt(k) for all 0 <= k < str.length(). >Also, str.equals(sbd.toString()) returns true. IOW, a String value assigned to a StringBuilder variable. >> [...] >> Am I missing something about StringBuilder, or is it really this >> difficult to play with? > > You've been advised to read some Java tutorials or textbooks, >and (it seems) have chosen not to follow that advice. Under the >circumstances, then, I'd have to say StringBuilder is "really this >difficult to play with." Which I have. I have been referring to the docs for StringBuilder, but the docs are rather incomplete, and I have to guess. This takestime. > Perhaps you're "too pressed for time" to learn Java before writing >it. Okay, yeah, that's reality, shit happens. But if so, you've been >"pressed for time" for some weeks now, and (I'll wager) have already A few and part-time. >wasted more time chasing blind alleys than you would have spent learning >the rudiments of the language. (A potential infinite regress looms.) I know the rudiments. I am having trouble with the next level. I have been asking here to try to knock out some of those blind alleys. I can get something to work, but I would prefer to have something work well. >> It would make a lot more sense to me if >> StringBuilder worked more like String does. > > StringBuilder is only of interest because it is mutable, while >String is immutable. From this fact flow many of the necessary >differences that give you so much trouble, but that others take in >stride. Why is the syntax so different? It does not need to be, but it is. In order to change from String to StringBuilder, I had to modify a bunch of code apart from changing variable types. > Summary: Fer Crissakes, Gene, read the damn' book! I have been RTFM. The docs often say what, but without an example and usually not why. I am paddling the best I can. Please do not cannonball next to me. Sincerely, Gene Wirchenko