1. First, string does not belong to eight basic data types. String is an object.
Because the default value of an object is null, the default value of string is also null, but it is a special object and has some features that other objects do not have.
2. Both new string () and new string ("") declare a new null string, which is an empty string or not null;
3. String STR = "kvill ";
String STR = new string ("kvill"); difference:
Here, we will not talk about heap or stack, but simply introduce the simple concept of constant pool.
The constant pool refers to the data that is identified during the compilation period and saved in the compiled. Class file. It includes constants in classes, methods, interfaces, and other fields, as well as string constants.
Example 1:
String S0 = "kvill ";
String S1 = "kvill ";
String S2 = "KV" + "ill ";
System. Out. println (S0 = S1 );
System. Out. println (S0 = S2 );
Result:
True
True
First, we need to know that Java will ensure that a String constant has only one copy.
In this example, "kvill" in S0 and S1 are both string constants and are determined during the compilation period. Therefore, S0 = S1 is true, while "KV" and "ill" are also
It is a String constant. When a string is connected by multiple string constants, it must also be a String constant. So S2 is also resolved as a String constant during compilation, so S2 is also common
A reference of "kvill" in the volume pool.
So we get S0 = S1 = S2;
The string created with new string () is not a constant and cannot be determined during the compilation period. Therefore, the strings created with new string () are not placed in the constant pool and they have their own address space.
Example 2:
String S0 = "kvill ";
String S1 = new string ("kvill ");
String S2 = "KV" + new string ("ill ");
System. Out. println (S0 = S1 );
System. Out. println (S0 = S2 );
System. Out. println (S1 = S2 );
Result:
False
False
False
In Example 2, S0 is still a "kvill" application in the constant pool. S1 is a reference to the new object "kvill" created during the runtime because it cannot be determined during the compilation period. S2 has the latter half.
The new string ("ill") cannot be determined during the compilation period, so it is also an application that creates the "kvill" object. If you understand this, you will know why this result is obtained.
4. String. Intern ():
Another note: the constant pool exists in the. Class file and is loaded by JVM at runtime and can be expanded. The intern () method of string is an extended constant.
A Method of the pool. When a string instance STR calls the intern () method, Java queries whether the constant pool has string constants of the same UNICODE. If yes, it returns
If not, add a string whose Unicode is equal to STR in the constant pool and return its reference. See example 3.
Example 3:
String S0 = "kvill ";
String S1 = new string ("kvill ");
String S2 = new string ("kvill ");
System. Out. println (S0 = S1 );
System. Out. println ("***********");
S1.intern ();
S2 = s2.intern (); // assign the reference of "kvill" in the constant pool to S2
System. Out. println (S0 = S1 );
System. Out. println (S0 = s1.intern ());
System. Out. println (S0 = S2 );
Result:
False
**********
False // although s1.intern () is executed, its return value is not assigned to S1
True // indicates that s1.intern () returns a reference to "kvill" in the constant pool.
True
Finally, let me get rid of another misunderstanding:
Someone said, "using the string. Intern () method, you can save a string class to a global string table.
If the Unicode string is already in this table, this method returns the address of the existing string in the table. If there is no string with the same value in the table, register your address in the table.
If a global string table is understood as a constant pool, its last sentence is "if there is no string with the same value in the table, register your own address to the table" is incorrect:
Example 4:
String S1 = new string ("kvill ");
String S2 = s1.intern ();
System. Out. println (S1 = s1.intern ());
System. Out. println (S1 + "" + S2 );
System. Out. println (s2 = s1.intern ());
Result:
False
Kvill
True
In this class, we do not have a "kvill" constant, so there is no "kvill" in the constant pool at the beginning. When we call s1.intern (), it will be in the constant pool.
A new "kvill" constant is added. The original "kvill" that is not in the constant Pool still exists, so it is not "registering your own address to the constant pool.
If S1 = s1.intern () is false, the original "kvill" still exists;
S2 is now the address of "kvill" in the constant pool, so S2 = s1.intern () is true.
5. About equals () and =:
In simple terms, this is to compare whether the unicode sequence of the two strings is equivalent. If the two strings are equal, true is returned, and = is to compare whether the addresses of the two strings are the same, that is, whether it is a reference to the same string.
6. the string is immutable.
This is a lot more important, as long as you know that the string instance will not change once it is generated, for example: String STR = "KV" + "ill" + "" + "Ans ";
There are four string constants. First, "KV" and "ill" generate "kvill" in the memory, and then "kvill" and "generate" kvill
"In memory, and finally generate" kvill
ANS ", and the string address is assigned to STR, because the string's" immutable "produces many temporary variables, that is why we recommend using the original stringbuffer
Because stringbuffer can be changed.