Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipse-dev] Beware of String#substring(...)

I just noticed that String#substring(...) shares the underlying char array.
So if you have a big string, take a substring of it, throw away the big
string, you will still hold on the big char array.
A simple way to solve this is to make a copy of the substring using the
String(String) constructor.

I saved 550KB in JDT Core by changing the following code:
      String simpleName =
fullyQualifiedName.substring(fullQualifiedName.lastIndexOf('.'));
to
      String simpleName = new
String(fullyQualifiedName.substring(fullQualifiedName.lastIndexOf('.')));

Jerome



Back to the top