Arne Vajhøj said:
I would not worry about temporary object being created
unless I observe an actual problem.
I had considered this thread done already, but since there is further
interest...
There's really two aspects to my original problem.
1) will any temporary object be created when append()ing a CharSequence
to a StringBuilder?
2) might StringBuilder eventually call a CharSequence's toString() from
within it's .append()?
A positive answer for 2) would also imply "yes" for 1), of course.
Given the *current* implementation of Oracle's implementation,
the answer to both questions is (fortunately) negative.
Even the consequences of a "1) yes 2) no" reply would not really be
as "earth-shattering" as I might have implied so far in this thread.
Now, for question 2)
There'll be essentially two kinds of CharSequence implementations:
a) some kind of "mutable String-alike", like e.g. StringBuilder.
these likely save their data in some char[] already, and
creating a String of that is trivial.
b) those that "calculate" each char in charAt().
My ChSeq falls into that category, and maybe a couple other
special purpose ones.
For those of kind b), being allowed to slam "this" to a StringBuilder
and then have the StringBuilder create a String object, would sound like
a nice example of code-reuse, compared to having to write the loop and
append-call for each char.
I don't really have any idea, how many CharSequence implementations
of kind b) really exist in the world, and it is not unlikely, that
they are far too few to justify any thought at all, but nevertheless,
I wish that StringBuilder.append() were SPECIFIED to use CharSequence-
API (i.e. essentially length() and charAt(), as is already implemented)
to SAFELY allow for trivial .toString() implementations in those classes.