The previous understanding of string is limited to three points:
1. string is a final class and cannot be inherited
2. Comparison strings of the string type must be equal to "equals" and cannot be "="
3. the string class cannot be changed.
String is very convenient to use. Therefore, this class is generally used for string processing when a string is involved.
As for the mechanism of the class of the string class, it is seldom explored.
Until you read the following example.
Class X
{
Public static string strx = "hello ";
}
Class y
{
Public static string stry = "hello ";
}
Class Z
{
Public static string strz = "hell" + "O ";
}
Class Test
{
Public static void main (string [] ARGs)
{
System. Out. println (X. strx = Y. stry );
System. Out. println (X. strx = Z. strz );
String S1 = "El ";
String S2 = "Lo ";
System. Out. println (X. strx = (S1 + S2 ));
System. Out. println (X. strx = (S1 + S2). Intern ());
}
}
According to my understanding, the three constant strings in Class X, class y, and Class Z belong to different objects. If we use the = Operator to compare them, the result must be false. The output should be:
False
False
False
False
But the actual output is:
True
True
False
True
I am confused. So I found a lot of information (especially C ++ and Java compared programming, which explains in detail) and finally gradually understood the cause.
= The value is true only when two comparison objects point to the same object. The test results of X. strx = Y. stry and X. strx = Z. strz are true, indicating that all three constants point to the same object.
In Java, string is a String constant. Two strings composed of characters of the same sequence belong to the same object and are located in the same position in the memory. Each String constant is represented by only one string object. Even if this string constant appears in different places of a program or even different packages of a program. That is to say, because X. strx and Y. stry are both of the same character sequences, they must point to the same object.
The same is true for Z. strz. "Hell" + "O", after the two constant strings are added, A New String constant is created (this is completed during compilation), which corresponds to X. strx, Y. stry has the same character sequence, so it points to the same object.
Although a constant expression links two string constants in the compiler
String S1 = "El ";
String S2 = "Lo ";
String S3 = S1 + S2;
In this Code, S3 can only be created during running. The "hello" string created by these three statements is an independent location where the string object is stored in the memory.
Java provides a mechanism to add a runtime string to the String constant pool through the intern () method defined by the string class (if it is not in the pool ). If the preceding three codes are changed:
String S1 = "El ";
String S2 = "Lo ";
String S3 = (S1 + S2). Intern ();
Java compares the character sequence in the string object returned by s1 + S2 with the string already stored in the String constant pool. If a match is found, intern () returns the reference of this string. If no match is found, the string returned by s1 + S2 will be added to the String constant pool and the reference of this string constant will be returned.
This explains how to return false for x. strx = (S1 + S2), and true for X. strx = (S1 + S2). Intern. (S1 + S2). Intern () points to the strx, stry, strx points to the String constant "hello ".