I've been told that code such as:
for (int i=0; i<x.length(); i++) {
// blah
}
is actually O(n^2) because of the repeated calls to x.length(). Instead I should use:
int l=x.length();
for (int i=0; i<l; i++) {
// blah
}
Is this true? Is string length stored as a private integer attribute of the String class? Or does length() really walk the whole string just to determine its length?