Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #25345

Re: StringBuilder.append and CharSequence

From markspace <markspace@nospam.nospam>
Newsgroups comp.lang.java.programmer
Subject Re: StringBuilder.append and CharSequence
Date 2013-09-25 18:34 -0700
Organization A noiseless patient Spider
Message-ID <l202uq$ae1$1@dont-email.me> (permalink)
References <slrnl45s71.mol.avl@gamma.logic.tuwien.ac.at> <l1vfg6$mi$1@dont-email.me>

Show all headers | View raw


On 9/25/2013 1:02 PM, markspace wrote:
> On 9/25/2013 7:19 AM, Andreas Leitgeb wrote:
>> Is it safe to use "stringBuilder.append(this);"
>
> It wasn't clear to me either from reading the Java docs that the above
> was safe, so I'd avoid it.
>
>> in a CharSequence
>> implementation's .toString(), or could this lead to a recursive
>> loop, if some version of a Java class library implements the
>> append() for CharSequence by using it's toString() ?
>
> That paragraph wasn't clear to me at all.  I was going to suggest
> toString() as the safe approach.
>
>    stringBuilder.append( this.toString() );
>
> This seems bomb proof to me, as the string is constructed before the
> method call.  Did I miss something?

Actually, I mis-read your problem statement, I think.  Having 
implemented a couple of CharSequence classes, the only rational approach 
would be to copy the chars out of a CharSequence one at a time with a 
loop and charAt().  toString() must create an immutable String and so 
must create a new buffer, so that's an extra buffer copy, which seems 
undesirable no matter who's implementing the append() method.  However I 
wouldn't call toString(), as that would obviously create a stack overflow.

I would think that you must have some much better method of copying 
characters though, as you should have access to your own private fields.

   @Override
   public String toString() {
     StringBuilder sb = new StringBuilder();
     sb.append( myCharArray, start, length );
     return sb.toString();
   }

This obviously could be done with a single "new String(..." but I'm just 
trying to hammer the idea home that there's internal fields to use.


Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

StringBuilder.append and CharSequence Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-09-25 14:19 +0000
  Re: StringBuilder.append and CharSequence markspace <markspace@nospam.nospam> - 2013-09-25 13:02 -0700
    Re: StringBuilder.append and CharSequence markspace <markspace@nospam.nospam> - 2013-09-25 18:34 -0700
      Re: StringBuilder.append and CharSequence Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-09-26 17:37 +0000
        Re: StringBuilder.append and CharSequence Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2013-09-26 23:11 +0200
          Re: StringBuilder.append and CharSequence Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-09-27 13:30 +0000
            Re: StringBuilder.append and CharSequence Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2013-09-27 19:31 +0200
              Re: StringBuilder.append and CharSequence markspace <markspace@nospam.nospam> - 2013-09-27 13:45 -0700
                Re: StringBuilder.append and CharSequence Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-09-27 23:16 +0000
                Re: StringBuilder.append and CharSequence Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2013-09-29 04:58 +0200
                Re: StringBuilder.append and CharSequence Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-09-30 20:02 +0000
  Re: StringBuilder.append and CharSequence Arne Vajhøj <arne@vajhoej.dk> - 2013-09-25 20:57 -0400
    Re: StringBuilder.append and CharSequence Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-09-26 18:01 +0000
      Re: StringBuilder.append and CharSequence Arne Vajhøj <arne@vajhoej.dk> - 2013-09-26 15:47 -0400
        Re: StringBuilder.append and CharSequence Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-09-26 20:48 +0000
          Re: StringBuilder.append and CharSequence Arne Vajhøj <arne@vajhoej.dk> - 2013-09-26 16:58 -0400
            Re: StringBuilder.append and CharSequence Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-09-27 12:52 +0000
              Re: StringBuilder.append and CharSequence Arne Vajhøj <arne@vajhoej.dk> - 2013-10-04 20:59 -0400
                Re: StringBuilder.append and CharSequence Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-10-05 12:51 +0000
  Re: StringBuilder.append and CharSequence Sven Köhler <remove-sven.koehler@gmail.com> - 2013-09-27 15:41 +0300

csiph-web