3

Using a profiler, I seem to be seeing the following with Apple's 1.6 Java:

I start with a moderately long Java string. I split it into tokens using String.split("\\W+"). The code then holds references to some of the split up pieces.

It seems, if I believe my eyes in yourkit, that Java has helpfully not copied these strings, so that I'm in fact holding references to the lengthy originals. In my case this leads to a rather large waste of space.

Does this seem plausible? It's easy enough to add a loop making copies of these guys.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
  • 1
    Your suspicion is correct. See Jon Skeet's comment on http://blogs.msdn.com/b/ericlippert/archive/2011/07/19/strings-immutability-and-persistence.aspx – SLaks Oct 02 '11 at 21:06
  • String.substring does the same – Joel Oct 02 '11 at 21:14
  • Related: [What is the purpose of the expression "new String(…)" in Java?](http://stackoverflow.com/questions/390703) – McDowell Oct 02 '11 at 21:45

1 Answers1

4

String.split() does not copy the parts of the String [the new objects...], instead it uses the String's fields: offset and count. By "changing" them, when later you access the String object, it is done by adding the offset to the original reference. This is indeed done to prevent copying the whole String, and save space [well, at least usually...].
So basically yes. All of your new objects, will have the same char[] reference, which leads to the original char[], in the original String.

amit
  • 175,853
  • 27
  • 231
  • 333