Java Reference
In-Depth Information
// Make string1 and string3 refer to separate strings that are identical
string1 += string2;
string1 = string1.intern(); // Intern string1
The
intern()
method checks the string referenced by
string1
against all the
String
objects currently
in existence. If it already exists, the current object is discarded, and
string1
contains a reference to the ex-
isting object encapsulating the same string. As a result, the expression
string1 == string3
evaluates to
true
, whereas without the call to
intern()
it evaluated to
false
.
All string constants and constant
String
expressions are automatically interned. That's why
string1
and
string3
would reference the same object if you were to use the same initializing string literal. Suppose
you add another variable to the previous code fragment:
String string4 = "Too " + "many ";
The reference stored in
string4
is automatically the same as the reference stored in
string1
. Only
String
expressions involving variables need to be interned explicitly by calling
intern()
. You could have
written the statement that created the combined string to be stored in
string1
with this statement:
string1 = (string1 + string2).intern();
This now interns the result of the expression
(string1 + string2)
, ensuring that the reference stored
in
string1
is unique.
String interning has two benefits. First, it reduces the amount of memory required for storing
String
ob-
jects in your program. If your program generates a lot of duplicate strings then this is significant. Second,
it allows the use of
==
instead of the
equals()
method when you want to compare strings for equality.
Because the
==
operator just compares two references, it is much faster than the
equals()
method, which
involves a sequence of character-by-character comparisons. This implies that you may make your program
run much faster, but only in certain cases. Keep in mind that the
intern()
method has to use the
equals()
method to determine whether a string already exists. More than that, it compares the current string against
a succession of, and possibly all, existing strings in order to determine whether the current string is unique.
Realistically, you should stick to using the
equals()
method in the majority of situations and use interning
only when you are sure that the benefits outweigh the cost.
WARNING
If you use string interning, you must remember to ALWAYS intern your strings.
Otherwise the == operator will not work properly.
Checking the Start and End of a String
It can be useful to be able to check just part of a string. You can test whether a string starts with a particular
character sequence by using the
startsWith()
method for the
String
object. The argument to the method
is the string that you want to look for at the beginning of the string. The argument string can be of any length,
but if it's longer than the original string you are testing, it will always return
false
. If
string1
has been
defined as
"Too many cooks"
, the expression
string1.startsWith("Too")
has the value
true
. So would
the expression
string1.startsWith("Too man")
. Here's an example of using this method:
String string1 = "Too many cooks";
if(string1.startsWith("Too")) {
System.out.println("The string does start with \"Too\" too!");