Full parsing of String object data types in Java and full parsing of string
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 a null String or not;
3. String str = "kvill ";
String str = new String ("kvill"); differences:
Here, we will not talk about heap or stack, but simply introduce the simple concept of constant pool.
A constant pool is determined during the compilation and saved in the compiled state. Some data in the class file. It includes constants in classes, methods, interfaces, and other fields, as well as string constants.
Example 1:
Copy codeThe Code is as follows:
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 this example are both string constants, they are determined during the compilation period, so s0 = s1 is true; both "kv" and "ill" are string constants. When a string is connected by multiple string constants, it must be a String constant, so s2 is also parsed as a String constant during compilation, so s2 is also a reference of "kvill" in the constant 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:
Copy codeThe Code is as follows:
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 an "kvill" application in the constant pool. s1 is a reference to the new object "kvill" created at runtime because it cannot be determined during the compilation period, s2 is also an application that creates the "kvill" object because there is a second part of newString ("ill") that cannot be determined during the compilation period; if you understand this, you will know why this result is obtained.
4. String. intern ():
I would like to add another note: it exists in. The constant pool in the class file is loaded by JVM at runtime and can be expanded. The intern () method of String is a method 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 reference is returned. If no, a string with Unicode equal to str is added to the constant pool and Its Reference is returned. See example 3.
Example 3:
Copy codeThe Code is as follows:
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, "Use String. the intern () method can save a String class to a global String table. If a Unicode String with the same value is already in this table, this method returns the address of an existing String in the table, if there is no String with the same value in the table, register your address to the table. "If I understand this global String table as a constant pool, his last sentence is, "If there is no string with the same value in the table, register your address to the table" is incorrect:
Example 4:
Copy codeThe Code is as follows:
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 () then, a new "kvill" constant is added to the constant pool. 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 memory, and then "kvill" and "generate" kvill "in memory, finally, "kvill ans" is generated, and the address of this String is assigned to str, because the String "immutable" produces many temporary variables, this is why StringBuffer is recommended, because StringBuffer can be changed.