tags:

views:

432

answers:

3

I recently discovered that the java.lang.String.substring method does not return a new string, but a view of the original string that was substringed. This can have memory implications. For example, if you're reading an ascii file, and parsing tokens in the file using substring and storing the result of substring in memory somewhere -- what you're actually storing in memory is the entire string prior to the substring operation! You can of course solve this by wrapping substring in your own version that returns a new string of the subtring result.

A: 

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html

In the java docs, it states that the substring method does return a new string.

Or did I misunderstand the question?

Also, strings are immutable. Here's a SO thread that explains why that is so.

Sev
it's a new string object, but the underlying data can be shared
Javier
+3  A: 

I've been bitten by it once, reading a dictionary file line by line. Each line was very short, but the buffer created by BufferedReader meant that each string was backed by an 80-char array.

That was when I first learned the point of writing:

word = new String(word);

Most of the time it's not a problem though - and of course it can be more efficient than the "take a completely separate copy" approach.

Jon Skeet
more computationally efficient, yes.
Amir Afghani
And potentially more efficient in memory too - if you still need the original string, or potentially many overlapping substrings.
Jon Skeet
A: 

In the year 2000 or 2001, one of the early XML parsers (I can't remember which) suffered from this problem. It took us a while to track down how we were going out of memory by reading about 3 fields out of some early FpML (very big XML documents which describe financial products).

What is really annoying is that if a write

String copy = new String(s);

IntelliJ IDEA warns me that this is redundant! Stupid IDE.

oxbow_lakes
Sounds like somebody over at JetBrains needs to read up on String interning
Nick Holt