CharSequence.subSequence optimizations

Aleksey Shipilev aleksey.shipilev at
Thu Oct 29 17:46:39 UTC 2015

On 10/29/2015 01:36 PM, Pavel Rappo wrote:
>> On 29 Oct 2015, at 00:48, Aleksey Shipilev <aleksey.shipilev at> wrote:
>> Doesn't that break when CharSequence implementation is mutable?
>> E.g. with StringBuilder, you cannot return "this" when "end ==
>> StringBuilder.length()" at the time of .subSequence() call.
> True. This trick cannot be used in cases where CharSequence is able to change
> its length. (As far as I understand, possible changes in contents are ok.)

While Javadoc appears silent on this front, the expectation seems to be
that .subSequence returns the "detached" subsequence. See spec change here:

So, I think this trick works only for the completely immutable
CharSequences, which apparently includes only Strings. And, AFAIU from
the code, String already returns "this" in cases like these?


P.S. I think it would be evil to turn subSequence into the forwarding
view. These were considered before:

More information about the core-libs-dev mailing list