C
Chris Smith
Roedy said:Hmm. The String class code has changed. The new code avoids pinning
large blocks of RAM, but it does more System.arrayCopys.
Nope, not the case, unless you're thinking of something different from
me. The issue here has always been that String.substring resulted in
saving the entire character data from the original String, and that
still happens. The code you quoted:
public String substring(int beginIndex, int endIndex) {
[argument checking]
return ((beginIndex == 0) && (endIndex == count)) ? this :
new String(offset + beginIndex, endIndex - beginIndex,
value);
}
Note that, assuming this isn't the trivial case of a no-op substring,
the functionality comes down to the constructor String(int,int,char[]).
Perhaps you're confusing that with String(char[],int,int) -- but they
are really quite different. The former constructor, which is used here,
does not perform a single copy of the data at all, but merely reuses the
same array, but creates a new value with different offsets and lengths
so as to preserve the character array.
So the same problem still exists.
The constructor that you quote, on the other hand, does make a copy if
the underlying data is larger than what is desired. That's a special
feature of that constructor, not something that's inherent to all
constructors.
The question now is, when did it change?
And the answer, I think, is that it never did.
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation