3 ms·
> Java 7 quietly changed the structure of String. Instead of an offset and a count, the String now only contained a char[]. This had some harmful effects for th
by ExpiredLink 11y ago
> Java 7 quietly changed the structure of String. Instead of an offset and a count, the String now only contained a char[]. This had some harmful effects for those expecting substring() would always share the underlying char[].
In theory they could have implemented both. Substring optimization for small but not larger strings.
- aembleton 11y agoThat would been terrible and unexpected. The behaviour would differ based upon the length of the input String.
- david-given 11y agoIt shouldn't. The behaviour is specified by the external interface, and if the internal implementation is changing the behaviour, then the implementation is broken. For example, back when I was at a JVM company, we had two string implementations, one for ASCII strings and one for UCS-2 strings; the JVM uses a lot of ASCII strings, and frequently you could figure this out at code load time. Having an implementation based on an array of bytes saved quite a lot of space, and was completely transparent.
- bsdetector 11y agoThe behavior would differ, but it would be the better behaviors for the length of the String. For large strings, it would work exactly like it did before with fast views. And since the existing code already knew about memory sharing this wouldn't be a new problem for any code. For small strings, it would now be faster and have smaller memory footprint. Existing programs would be faster and better performing. I think the main reason this is a bad idea is that String is final. The way to do this properly is with two classes, SmallString and LargeString, and then using hotspot to optimize the program for how they are used. But that is not possible without changing the API.