Skip to main content

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

Along the same line, when creating String from some char[] fragment, you
should prefer doing:

  char[] original = ...;
  new String(original, start, length);

instead of:

  char[] original = ...;
  char[] fragment;
  System.arraycopy(original, 0, fragment = new char[length], start,
length);
  new String(fragment);

This saves the transient char[] allocation.




                                                                           
             Jerome                                                        
             Lanneluc/France/I                                             
             BM@IBMFR                                                   To 
             Sent by:                  eclipse-dev@xxxxxxxxxxx             
             eclipse-dev-admin                                          cc 
             @eclipse.org                                                  
                                                                   Subject 
                                       [eclipse-dev] Beware of             
             03/25/2005 01:22          String#substring(...)               
             PM                                                            
                                                                           
                                                                           
             Please respond to                                             
                eclipse-dev                                                
                                                                           
                                                                           




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

_______________________________________________
eclipse-dev mailing list
eclipse-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe
from this list, visit
http://dev.eclipse.org/mailman/listinfo/eclipse-dev




Back to the top