Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-8030311 | 8u5 | Stuart Marks | P4 | Resolved | Fixed | b01 |
The spec for CharSequence.subSequence says,
Returns a new CharSequence that is a subsequence of this sequence.
This is overspecified. For String, subSequence should be allowed to return a pre-existing instance (such as 'this' or perhaps an interned string or something) as long as it has the right value.
This line of the specification was probably intended to apply to mutable CharSequences, as the mutability pretty much requires subSequence to return a new instance in order to avoid confusion. All the implementations of CharSequences in the JDK are mutable, except for String.
It might be reasonable to modify the spec to be something like this:
<< Returns a CharSequence that is a subsequence of this sequence. The returned sequence must be an instance such that mutation of this sequence or the returned sequence must not affect the value of the other. >>
This could be wordsmithed, of course. This pretty much has the effect of requiring mutable CS instances to return new copies, but String can return any instance it pleases since Strings are immutable.
Note that the current implementation of String.subSequence() simply calls String.substring(). This in turn returns 'this' if the substring range consists of the entire String. Thus, String.subSequence violates this specification, because it does not return a "new" CharSequence in some circumstances.
Returns a new CharSequence that is a subsequence of this sequence.
This is overspecified. For String, subSequence should be allowed to return a pre-existing instance (such as 'this' or perhaps an interned string or something) as long as it has the right value.
This line of the specification was probably intended to apply to mutable CharSequences, as the mutability pretty much requires subSequence to return a new instance in order to avoid confusion. All the implementations of CharSequences in the JDK are mutable, except for String.
It might be reasonable to modify the spec to be something like this:
<< Returns a CharSequence that is a subsequence of this sequence. The returned sequence must be an instance such that mutation of this sequence or the returned sequence must not affect the value of the other. >>
This could be wordsmithed, of course. This pretty much has the effect of requiring mutable CS instances to return new copies, but String can return any instance it pleases since Strings are immutable.
Note that the current implementation of String.subSequence() simply calls String.substring(). This in turn returns 'this' if the substring range consists of the entire String. Thus, String.subSequence violates this specification, because it does not return a "new" CharSequence in some circumstances.
- backported by
-
JDK-8030311 (spec str) CharSequence.subSequence improperly requires a "new" CharSequence be returned
- Resolved
- relates to
-
JDK-8138614 (spec str) StringBuffer and StringBuilder methods improperly require "new" String to be returned
- Open
-
JDK-7174936 (spec str) several String methods claim to always create new String
- Closed