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.
Constant pool (constant
Pool) refers to some data that is determined during the compilation period and stored 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.
Because "kvill" in S0 and S1 in the example are string constants, they are determined during the compilation period, so S0 = S1 is true; both "KV" and "ill"
Is a String constant. When a string is connected by multiple string constants, it must also be a String constant. So S2 is also parsed as a String constant during compilation, so S2 is also a constant.
A reference of "kvill" in the 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 new string ()
The created strings are not placed in the constant pool. 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 runtime because it cannot be determined during the compilation period. S2 has the second half.
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 to expand the constant pool.
When a string instance STR calls the intern () method, Java checks whether the constant pool has the same UNICODE String constant. If yes, its
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 of "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 (), we 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 in 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 =:
This is simply to compare the unicode sequence of the two strings. If the two strings are equal, true is returned, and = is to compare whether the addresses of the two strings are the same.
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" exists in the memory, and finally generates "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.