Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: Java blunders Date: Mon, 28 Jul 2014 16:39:27 -0400 Organization: A noiseless patient Spider Lines: 61 Message-ID: References: <53cef75d$0$291$14726298@news.sunsite.dk> <7s6vs9p395bretqrtdvh9i6til83vfk52p@4ax.com> <53d04b5f$0$291$14726298@news.sunsite.dk> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Mon, 28 Jul 2014 20:39:27 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="bd4e1cba8d8592bec5d5ed9e4537fbfd"; logging-data="29129"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19/1T8djGtHP528A4Irwq81" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: Cancel-Lock: sha1:s+7cyX08eMZDTMxYjm6YdB5G9Js= Xref: csiph.com comp.lang.java.programmer:29406 On 7/28/2014 4:23 PM, Josip Almasi wrote: > On 07/24/2014 03:53 AM, Roedy Green wrote: >> On Wed, 23 Jul 2014 19:55:09 -0400, Arne Vajhøj >> wrote, quoted or indirectly quoted someone who said : >> >>> But is it a problem in real world programming? >>> >>> I have never seen a problem due to this. >> >> >> The most common error is like this: >> >> out.println ( "the answer is " + >> a + b >> + "."); >> >> you must write that as: >> >> out.println ( "the answer is " + (a + b) + "."); > > I think you guys missed worst thing with java concatenation, and it's > got nothing to do with operator. > It's about how operator gets interpreted: > > out.println ( new StringBuffer("the answer is ").append(new > StringBuffer(a + b)).append(new StringBuffer(".")); Why do you think this is so? javac 1.8.0_11 compiles it to the equivalent of out.println(new StringBuilder() .append("the answer is ") .append(a + b) .append(".") .toString()); ... and I think all javac's have done so for a long time (but I don't have them available for checking). > It's three new objects, all synchronized. ... for suitable values of "three" (i.e., 2) and "all" (0). > OK I haven't read the spec for a while but that's it IIRC, spec requires > it that way. StringBuilder replaced StringBuffer for this purpose in Java 1.5, just under ten years ago. I guess that counts as "a while." ;-) > And if you want to avoid the garbage, you need to > > StringBuilder shit = new StringBuilder(); > shit.append("the answer is "); > shit.append(a + b); > shit.append("."); > and another line to get the string. Um, er, no. -- esosman@comcast-dot-net.invalid