One, fundamentally understand java.lang.String class and string pool
First, I suggest that you first look at the string class's source implementation, which is the fundamental starting point of knowing the string class in essence. You can see:
The 1,string class is final and cannot be inherited. Public final class String.
The 2,string class is essentially a character array char[], and its value cannot be changed. Private final char value[];
You then open the API documentation for the string class to discover:
The 3,string class object has a special way of creating, which is to directly specify such as String x = "abc", and "abc" represents a String object. And x is the address of the "ABC" object, also known as a reference to an "ABC" object.
The 4,string object can be concatenated through the "+" concatenation. A new string is generated after concatenation. It can also be concatenated by concat (), which is described later. 5, there are many ways to create strings, summed up in three categories:
First, use the New keyword to create a string, such as String S1 = new String ("abc");
Second, direct designation. such as String s2 = "abc";
Third, a new string is generated using concatenation. For example, string s3 = "AB" + "C";
The 6,java runtime maintains a string pool (string pooling), and Javadoc translation is very vague "string buffers." The string pool is used to hold the various strings generated in the runtime, and the contents of the strings in the pool are not duplicated. This buffer pool is not present in the normal object, and the object created is only in the stack area of the method. The following is a schematic of the system memory:
Second, the creation of a string object
String objects are also very important to create, and the key is to understand their rationale.
Principle 1: When you use any way to create a string object s, the Java runtime (the running JVM) takes this x in a string pool to find out if there is a string object with the same content, or if it does not, create a string s in the pool, otherwise, not in the pool.
In principle 2:java, whenever you create an object using the New keyword, you must create a new object (in the heap area or stack area).
Principle 3: To create a string object by using either direct designation or concatenation with a pure string, only the string in the maintenance string pool is checked, and no one in the pool is created in the pool. But never again creates the string object in the stack area.
Principle 4: Creating a String object with an expression that contains a variable not only checks the maintenance string pool, but also creates a string object in the stack area.
In addition, the Intern () method of String is a local method defined as public native String intern (); The value of the Intern () method is to allow developers to focus on the string pool. When the Intern method is invoked, the string in the pool is returned if the pool already contains a string that is equal to this string object, which is determined by the Equals (object) method. otherwise , this string object is added to the pool and a reference to this string object is returned.
Finally, there are a few questions to note: String A; It is equivalent to String a=null in the case of a class variable, while a local variable is different. NULL represents a null reference, String a=null means that a is declared on the stack, but this a does not point to any address. At this point we notice that string a declares a in the stack, But it doesn't point to any address, but the syntax check for Java if string A is not directly used in a local variable, this a in string a=null can be used directly.