Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!nx01.iad01.newshosting.com!newshosting.com!216.196.98.142.MISMATCH!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.posted.palinacquisition!news.posted.palinacquisition.POSTED!not-for-mail NNTP-Posting-Date: Sun, 18 Sep 2011 00:45:00 -0500 Date: Sat, 17 Sep 2011 22:44:59 -0700 From: Peter Duniho User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.2.22) Gecko/20110902 Thunderbird/3.1.14 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: StringBuilder References: <96f358c8-a024-40db-b60b-300186c2f813@o10g2000vby.googlegroups.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Lines: 30 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 50.46.118.188 X-Trace: sv3-Vbh1UhpsJGScucPgvgcQ3hjgq1q5BF7Zwn71WlstJEZgVJij51yXtGZgBEA66CudORTfcBq5SbhyKob!5eWYkq9l5vXSsZQcbfhuBwO1u/xQI/X1J9DvpPqBw9AjMFywdOexNg0+b8rW5xlOfaKCbYygM65d!pZOeDwuQ6gEeoqVSeMlI+Eg1o+mRsberR3i68TvwKK0= X-Complaints-To: abuse@iinet.com X-DMCA-Complaints-To: abuse@iinet.com X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2920 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:8116 On 9/17/11 3:34 PM, Roedy Green wrote: > [...] > StringBuilder composes its string in a char[]. Unfortunately it can't > simply plop that into a String object at the end. It has to allocate > yet another buffer, copy into it, and that becomes your string object. [...] Are you sure that's the implementation? In .NET, there is also a String and StringBuilder class pair, and StringBuilder.ToString() does in fact just create a new String instance using the existing buffer. It flags itself that it did so, and of course then it does have to allocate a new buffer if you mutate the StringBuilder instance later. But the most common scenario is to mutate the StringBuilder, then get the String instance just once, discarding the StringBuilder immediately after. I don't really care that much so I'm not going to waste time checking myself. But if anyone does think it's something to worry about, they should at least check the actual implementation to make sure it really does what they think it does. In any case, if Java does _not_ implement it that way, I suspect that's out of a different decision-making process rather than ignorance. In other words, they have already considered whether it's a worthwhile optimization and decided otherwise. Which strongly suggests that anyone worrying a priori about the performance of StringBuilder before they have demonstrated it's an actual bottleneck in their program is wasting their time. Pete