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: Daniele Futtorovic Newsgroups: comp.lang.java.programmer Subject: Re: StringBuilder.append and CharSequence Date: Sun, 29 Sep 2013 04:58:16 +0200 Organization: A noiseless patient Spider Lines: 44 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Injection-Date: Sun, 29 Sep 2013 02:58:25 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="f3bdcd9731e3bfbe655837259a60cc42"; logging-data="9967"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19+DUDLis+iEhHNWQk3yBz0" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130801 Thunderbird/17.0.8 In-Reply-To: Cancel-Lock: sha1:R+AfswXJfr1R0Kiov7vxbL1Chfk= Xref: csiph.com comp.lang.java.programmer:25385 On 2013-09-28 01:16, Andreas Leitgeb allegedly wrote: > markspace wrote: >> On 9/27/2013 10:31 AM, Daniele Futtorovic wrote: >>> On 2013-09-27 15:30, Andreas Leitgeb allegedly wrote: >>>> Daniele Futtorovic wrote: >>>>> I'd make the toString() as follows to be on the safe side: >>>>> char[] cs = new char[ m_len ]; >>>>> Arrays.fill( cs, m_ch ); >>>>> return new String(cs); >>>> If this is the ONLY safe side, then there wouldn't be a point >>>> in implementing a CharSequence in the first place for my task. >>> Why not? The Standard API's StringBuilder#append(CharSequence), AFAICS, >>> does not call toString() -- granted, the JavaDoc stipulates nothing to >>> that effect. >> >> I read Andreas's comment as "there's not point introducing a new type if >> a repeated string is just always turned into a regular String anyway." > > Yes, that's about it. > > The "new type" is the ChSeq from a few posts upthread. > The "repeated string" is a possibly long repetition of > just a single char, compactly represented by ChSeq... > Yes, but using the ChSeq (as it was introduced) to append it to a StringBuilder WILL NOT, as the JSE currently stands, result in the invocation of toString(). So you are safe to go. Then, as far as the implementation of toString() is concerned (which only matters if other API use your ChSeq's), I would say it ought to be implemented as above, for safety reasons and because, after all, since every String uses a char[] buffer, it stands to reason that you cannot produce a String, which toString() is supposed to do, without allocating such a buffer (as an optimisation, you could create it but once on demand). Incidentally, while I know no place in the documentation that expresses it, it is likely the case that all API designed to handle CharSequence's will operate on a by-char-basis (as opposed to calling toString() ) if they can afford it at all -- seeing how this is what the CharSequence interface is all about. -- DF.