Java Reference
In-Depth Information
for (i = 0; i < tableSize; i++)
if (table[i] == unique)
return i;
// it's not there--add it in
table[i] = unique;
tableSize++;
return i;
}
All the strings stored in the
table
array are the result of an
intern
invoc-
ation. The table is searched for a string that was the result of an
intern
invocation on another string that had the same contents as the
key
. If
this string is found, the search is finished. If not, we add the unique
representative of the
key
at the end. Dealing with the results of
intern
makes comparing object references equivalent to comparing string con-
tents, but much faster.
Any two strings with the same contents are guaranteed to have the
same hash codethe
String
class overrides
Object.hashCode
although two
different strings might also have the same hash code. Hash codes
are useful for hashtables, such as the
HashMap
class in
java.util
see
13.2.4. Making Related Strings
Several
String
methods return new strings that are like the old one but
with a specified modification. New strings are returned because
String
objects are immutable. You could extract delimited substrings from an-
other string by using a method like this one:
public static String delimitedString(
String from, char start, char end)
{
int startPos = from.indexOf(start);
int endPos = from.lastIndexOf(end);