Transferred from: http://lavasoft.blog.51cto.com/62575/80034/
Java String Class (Java.lang.String) is the most used class in Java, is also the most special class, many times, we are both familiar and unfamiliar.
first, fundamentally understand the Java.lang.String class and the string poolfirst, I propose to look at the source code implementation of the String class, which is the fundamental starting point to understand the string class in essence. From here you can see:1. The string class is final and cannot be inherited. Public final class String. 2, the String class is the essence of the character array char[], and its value cannot be changed. Private final char value[];then open the API documentation for the string class to discover:3, the String class object has a special way to create, is directly specified such as String x = "abc", "ABC" represents a String object. X is the address of the "ABC" object, also called a reference to the "ABC" object. 4. String objects can be concatenated by "+" and a new string is generated after concatenation. It can also be concatenated by concat (), which is explained later. 5. The Java runtime maintains a string pool, Javadoc translation is very vague "string buffer". The string pool is used to hold the various strings produced in the runtime, and the contents of the strings in the pool are not duplicated. The normal object does not exist in this buffer pool, and the object created only exists in the stack area of the method. The following is a system memory: 5, a lot of ways to create strings, summed up there are three categories:First , create a string using the new keyword, such as string S1 = new String ("abc");Second , direct designation. For example, string s2 = "abc";third, use concatenation to generate a new string. For example, string s3 = "AB" + "C";second, the creation of a string objectthe creation of String objects is also very important, the key is to understand its principle. Principle 1: When using any way to create a string object s, the Java runtime (the running JVM) will hold this s in the string pool to find out if there is a string object of the same content, if not, create a string s in the pool, otherwise, do not add in the pool. in principle 2:java, whenever you use the New keyword to create an object, you must create a new object (in the heap or stack area). principle 3: Creating a String object using direct designations or concatenation with a pure string only checks the string in the maintenance string pool without creating one in the pool. However, the string object will never be created in the stack area again. principle 4: Creating a String object with an expression containing a variable will not only examine the maintenance of the string pool, but also create a string object in the stack area. In addition, the Intern () method of string is a local method, defined as public native String intern (), and the value of the Intern () method is to allow the developer to focus on the String pool. When the Intern method is called, if the pool already contains a string equal to this string object (which is determined by the Equals (object) method), the string in the pool is returned. Otherwise, this string object is added to the pool, and a reference to this string object is returned. Third, examplesChang (constant pool) refers to some data that is determined at compile time and is saved in the compiled. class file. It includes constants in classes, methods, interfaces, and so on, and also includes string constants.
1 string s0=2 string s1=3 string s2= "kv" +4 System.out.println (s0==5
The result is:
True
True
First, we need to know that Java ensures that there is only one copy of a string constant.
Because the "Kvill" in S0 and S1 in the example are string constants, they are determined at compile time, so s0==s1 is true, and "kv" and "ill" are also string constants, and when a string is concatenated by multiple string constants, it is itself a string constant. So S2 is also parsed as a string constant at compile time, so S2 is also a reference to "Kvill" in the constant pool.
So we come to s0==s1==s2;
Strings created with new string () are not constants and cannot be determined at compile time, so the strings created by new string () are not placed in the constant pool, they have their own address space.
See Example 2:
1 string s0=2 string s1=new3new4 System.out.println (s0==5 System.out.println (s0==6
The result is:
False
False
False
In Example 2, S0 or "Kvill" in a constant pool, s1 because it cannot be determined at compile time, is a reference to the new object "Kvill" that was created at run time, S2 because there is a second part new String ("ill") so it cannot be determined at compile time, so it is also a newly created object " Kvill "The application of the", and understand that this will know why the result is obtained.
4. String.intern ():
One more point: the constant pool that exists in the. class file is loaded by the JVM at run time and can be expanded . The Intern () method of string is a method of extending a constant pool , and when a string instance str calls the Intern () method, Java looks for the same Unicode string constant in the constant pool, if any, Returns a reference to it, and if not, adds a string in the constant pool that is Unicode equals STR and returns its reference; See example 3 for clarity.
Example 3:
1String s0="Kvill";2String s1=NewString ("Kvill");3String s2=NewString ("Kvill");4System.out.println (s0==S1); 5System.out.println ("**********");6 S1.intern ();7S2=s2.intern ();//assign a reference to "Kvill" in the constant pool to S28System.out.println (s0==S1); 9System.out.println (s0==S1.intern ()); TenSystem.out.println (S0==S2);
The result is:
False
**********
False//Although S1.intern () is executed, but its return value is not assigned to S1
True//Description S1.intern () returns a reference to "Kvill" in a constant pool
True
Finally I break the wrong understanding:
Some say, "Using the String.intern () method, you can save a string class to a global string table, and if a Unicode string with the same value is already in the table, the method returns the address of the string already in the table. If you do not have a string of the same value in the table, register your own address in the table "If I interpret this global string table as Chang, his last word," if there are no strings of the same value in the table, then registering your own address in the table is wrong:
See Example 4:
1 string s1=new string ("Kvill"2 String s2=3 System.out.println (s1==4 System.out.println (s1+ "" +5
Results:
False
Kvill Kvill
True
In this class we do not have the reputation of a "kvill" constant, so there is no "kvill" in the constant pool, and when we call S1.intern () a new "Kvill" constant is added to the constant pool, and the original "Kvill" in the constant pool still exists. , it is not "registering your own address in a constant pool".
S1==s1.intern () to False indicates that 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 simple for string to compare whether the Unicode sequence of the two strings is equivalent, if equal returns true, and = = is the same as the address of the two strings, that is, a reference to the same string.
6. About the string is immutable
This said again a lot, as long as we know that the string instance once generated will not be changed, such as: string Str= "kv" + "ill" + "+" ans ";
There are 4 string constants, first "KV" and "ill" generate "Kvill" in memory, and then "Kvill" and "Generate" Kvill "in memory, and finally generated" Kvill ans ", and the address of the string to the STR, This is because the "immutable" string creates a lot of temporary variables, which is why it is recommended to use StringBuffer, because StringBuffer is a change.
The creation of a Java learning string