Parsing the data type of a string object in Java
1. String is an object.
Because the default value of the object is NULL, the default value of string is also null, but it is a special object that has some features that other objects do not have.
2. New string () and new String ("") are all declarations of a new empty string, that is, the empty string is not null;
3. String str= "Kvill";
String Str=new string ("Kvill"), the difference:
Here, we don't talk about heaps, or stacks, simply introduce the simple concept of a constant pool.
Chang (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.
See Example 1:
String s0= "Kvill";
String s1= "Kvill";
String s2= "kv" + "ill";
System.out.println (S0==S1);
System.out.println (S0==S2);
The result is:
True
True
First, we need to know the result. Java ensures that a string constant has only one copy.
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:
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);
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 a constant pool with the same Unicode string constant, if any, and returns its reference, if not, Adds a string of Unicode equal to STR in the constant pool and returns its reference; see example 3 for clarity.
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 a reference to "Kvill" in the constant pool to S2
System.out.println (S0==S1);
System.out.println (S0==s1.intern ());
System.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:
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 ());
Results:
False
Kvill Kvill
True
In this class we do not have the reputation of a "kvill" constant, so the constant pool in the beginning there is no "Kvill", when we call S1.intern () in the constant pool after the new "Kvill" constant, the original is not in the constant pool "Kvill" still exists, is not " Register your own address in the 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" of string produces a lot of temporary variables, which is why it is recommended to use StringBuffer, because StringBuffer can be changed.
The difference between string literals and new string in Java