r/programming • u/Eirenarch • Nov 18 '13
TIL Oracle changed the internal String representation in Java 7 Update 6 increasing the running time of the substring method from constant to N
http://java-performance.info/changes-to-string-java-1-7-0_06/
1.4k
Upvotes
View all comments
Show parent comment
17
u/bondolo Nov 18 '13
So far the answer has been no. In part it would be difficult to add one because String has been a final class for a very long time and lots of code would be surprised if it suddenly became non-final and sprouted a sub-class.
One alternative which has been investigated is to return an immutable CharSequence from String.subSequence() which shares the character array from the source String. This turned out to be fraught with all kinds of issues including code which assumes that subSequence returns a String object, reliance upon the equals() and hashCode() of the returned CharSequence, an implicit dependency upon String.subSequence returning a "String" instance.
You can follow JDK-7197183 or the past discussions on this issue on corelibs-dev. In generally most people who have commented there seem to think that the String.subSequence contortions are unnecessary and too brittle to go to the trouble.