8.10.04
09:16 java.util.String().substring() internals (
,
,
,
)
While debugging today I noticed that String.substring() doesn't extract the substring from the original String but copies the original String and just set a different offset and count of the new String.
I have tested the following code:
String helloWorld = "Hello new World" ;
String newHelloWorld = helloWorld.substring(6,9);
My debugger (Eclipse) gives me the following information:
- helloWorld:
- count = 15
- offset = 0
- value = char[15] = "Hello new World"
- newHelloWorld
- count = 3
- offset = 6
- value = char[15] = "Hello new World"
Checking the String.subString() code, it does the following:
new String(offset + beginIndex, endIndex - beginIndex, value);
and in String(offset, index, value) constructor the following is done:
this.value = value;
this.offset = offset;
this.count = count;
It seems like that you even don't get a copy of the original String (I first though this is done because of performance, arrayCopy being faster that extracting characters), but the new substring points to the same character array.
posted by Jean-Marc Autexier |
0 comments | Permalink | Send to Friends | Google it!
|