If it's at least half the size of the parent, reference the parent. Otherwise just make a copy...
If it's at least half the size of the parent, reference the parent. Otherwise just make a copy...
If you conditionally used one implementation or the other you'd lose the optimization opportunity. You could alternatively make string non-final; but then your method resolution is more expensive and that would really be a pretty big change in any case.
I think java made the right choice here since its trivial to wrap string in another structure whenever you need a fast substring, it's just that that custom string won't be passable to lots of other API's which makes it a potentially nasty surprise for existing codebases.
This change lets programmers not worry about the implementation of substring() and memory leaks in return for linear runtime complexity.