String constant pool in java
String constant pool details:
1. string uses private final char value [] to store strings. That is to say, after a String object is created, the String content stored in this object cannot be modified, the String type is immutable ). A special method for creating the String class is to use "" double quotation marks to create it. For example, new String ("I am") actually creates two
String object. One is created by "I am" through "" double quotation marks, and the other is created by new, except that they are created in different periods,
One is the compilation period and the other is the runtime period! Java reloads the + operator for the String type and can directly use + to connect two strings. You can call the intern () method of the String class at runtime to dynamically add objects to the String Pool.
Example 1
String s1 = "sss111 ";
// This statement is the same as above
String s2 = "sss111 ";
System. out. println (s1 = s2); // The result is true.
Example 2
String s1 = new String ("sss111 ");
String s2 = "sss111 ";
System. out. println (s1 = s2); // The result is false.
Example 3
String s1 = new String ("sss111 ");
S1 = s1.intern ();
String s2 = "sss111 ";
System. out. println (s1 = s2); // The result is true.
Example 4
String s1 = new String ("111 ");
String s2 = "sss111 ";
String s3 = "sss"> "111 ";
String s4 = "sss" + s1;
System. out. println (s2 = s3); // true
System. out. println (s2 = s4); // false
System. out. println (s2 = s4.intern (); // true
The above analysis is summarized as follows:
1. The strings created using the "" quotation marks are constants, and are stored in the String Pool at the compilation stage;
2. Objects created using new String ("") are stored in heap, which is newly created at runtime;
3. A String connector that only contains constants, such as "aa" + "aa", is created as a constant. It can be determined during compilation and stored in the String Pool;
4. Objects created using a string connector containing variables, such as "aa" + s1, are created at runtime and stored in heap;
There are also a few Frequently Asked Questions:
String s1 = new String ("s1 ");
String s2 = new String ("s1 ");
How many String objects are created above?
Answer: three, one in the Constant Pool during the compilation period, and two in the heap during the runtime. (an object is created on the heap every time a new object is created with the new object. If an object is created with the new object in the constant pool, it is directly directed to the object. You do not need to create the object)
String s1 = "s1 ";
String s2 = s1;
S2 = "s2 ";
What is the string in the object to which s1 points?
Answer: "s1 ". (Never forget that the String is unchangeable. s2 = "s2"; in fact, the direction of s2 has changed, because you cannot change a String ,)
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
String is a special packaging data. Available:
String str = new String ("abc ");
String str = "abc ";
The first method is to use new () to create an object, which is stored in the heap. Each call creates a new object.
The second method is to first create a String class object reference variable str in the stack, and then use the symbol reference to find out whether there is "abc" in the String constant pool. If no, store "abc" in the string constant pool and point str to "abc". If "abc" already exists, direct str to "abc ".
Use the equals () method to compare the values in a class. Use the = method to test whether the references of the two classes point to the same object. The example below illustrates the above theory.
String str1 = "abc ";
String str2 = "abc ";
System. out. println (str1 = str2); // true
It can be seen that str1 and str2 point to the same object.
String str1 = new String ("abc ");
String str2 = new String ("abc ");
System. out. println (str1 = str2); // false
The new method is used to generate different objects. Each time one is generated.
Therefore, the second method is used to create multiple "abc" strings, and only one object exists in the memory. this method is advantageous and saves memory space. at the same time, it can improve the program running speed to a certain extent, because the JVM will automatically decide whether to create a new object based on the actual situation of the data in the stack. For the code of String str = new String ("abc");, a new object is created in the heap, regardless of whether the String value is equal, whether it is necessary to create a new object, this increases the burden on the program.
On the other hand, Note: When we define classes using formats such as String str = "abc";, we always take it for granted that the str object of the String class is created. Worry Trap! The object may not be created! Instead, it may only point to a previously created object. Only by using the new () method can a new object be created every time.
Because of the immutable property of the String class, when the String variable needs to change its value frequently, you should consider using the StringBuffer class to improve program efficiency.
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")
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 can get s0 = s1 = s2; the String created with new String () is not a constant and cannot be determined during the compilation period, so 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 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.
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 says, "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 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 in 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 () 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.
Instance
Test1:
Package StringTest;
Public class test1 {
/**
* @ Param args
*/
Public static void main (String [] args ){
String a = "a1 ";
String B = "a" + 1;
System. out. println (a = B );
} // True
}
Test2:
Package StringTest;
Public class test2 {
/**
* @ Param args
*/
Public static void main (String [] args ){
String a = "AB ";
String bb = "B ";
String B = "a" + bb; // The compiler cannot be identified as a constant.
System. out. println (a = B );
} // False
}
Test3:
Package StringTest;
Public class test3 {
/**
* @ Param args
*/
Public static void main (String [] args ){
String a = "AB ";
Final String bb = "B ";
String B = "a" + bb; // after bb is added to final, it is a constant. You can determine B in the compiler.
System. out. println (a = B );
} // True
}
Test4:
Package StringTest;
Public class test4 {
/**
* @ Param args
*/
Public static void main (String [] args ){
String a = "AB ";
Final String bb = getBB ();
String B = "a" + bb; // bb is returned through the function. Although it is known that it is final, it does not know what it is. It is necessary to know the bb value at runtime.
System. out. println (a = B );
} // False
Private static String getBB () {return "B ";}
}
Test5:
Package StringTest;
Public class test5 {
/**
* @ Param args
*/
Private static String a = "AB ";
Public static void main (String [] args ){
String s1 = "";
String s2 = "B ";
String s = s1 + s2; // + usage
System. out. println (s = );
System. out. println (s. intern () = a); // meaning of intern
} // Flase true
}
Test6:
Package StringTest;
Public class test6 {
/**
* @ Param args
*/
Private static String a = new String ("AB ");
Public static void main (String [] args ){
String s1 = "";
String s2 = "B ";
String s = s1 + s2;
System. out. println (s = );
System. out. println (s. intern () = );
System. out. println (s. intern () = a. intern ());
} // Flase false true
}