Java improvements-understanding the application of String and String. intern () In practice, stringintern
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)It refers to some data that is determined 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.
Because "kvill" in s0 and s1 in the example are string constants, they are determined during the compilation period, so s0 = s1 is true; "kv" and "ill" are both 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:
1 String s0=”kvill”; 2 String s1=new String(”kvill”); 3 String s2=”kv” + new String(“ill”); 4 System.out.println( s0==s1 ); 5 System.out.println( s0==s2 ); 6 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 of 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 it has a new String ("ill") in the second half and cannot be determined during the compilation period; 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 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.
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 the address in the table. "If I understand the global String table he said as a constant pool, his last sentence is, "If there is no string with the same value in the table, register your address in the table" is incorrect:
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 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.
Reference link:
Functions of String pooling and intern
What is a constant pool? String. intern () when is it used? How to use it?