3 ms·
Part of the reason for the change was likely to allow better optimizations of the strings. The new strings are basically equivalent to arrays and might be opti
by emn13 13y ago
Part of the reason for the change was likely to allow better optimizations of the strings. The new strings are basically equivalent to arrays and might be optimized as such; even without that they certainly don't need an offset nor length field anymore since the underlying array can provide that.
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.
- cpeterso 13y agoCouldn't the substring copying be deferred until the parent string is ready to be GC'd? In the common case of a short-lived substring, no string copies are necessary. Only if the substring outlives the parent string would a copy be necessary.