1. First of all, String does not belong to the 8 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 characteristics that other objects do not have.
2. new String() and new String("") both declare a new empty string, which is an empty string and not null;
3. String str="kvill";
The difference between String str=new String ("kvill");:
Here, we will not talk about the heap or the stack, but simply introduce the simple concept of constant pool.
The constant pool refers to the pool that is determined at compile time and stored in the compiled pool. Some data in the class file. It includes constants in classes, methods, interfaces, etc., as well as string constants.
Look at example 1:
Copy the code code as follows:
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 that Java will ensure that there is only one copy of a string constant.
Because "kvill" in s0 and s1 in the example are both string constants, they are determined at compile time, so s0==s1 is true; and "kv" and "ill" are also string constants, When a string is concatenated by multiple string constants, it must itself be a string constant, so s2 is also parsed into a string constant at compile time, so s2 is also one of "kvill" in the constant pool Quote.
So we get s0==s1==s2;
Strings created with new String() are not constants and cannot be determined at compile time, so strings created with new String() are not put into the constant pool, they have their own address space.
Look at example 2:
Copy the code code as follows:
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 is still the application of "kvill" in the constant pool. Since s1 cannot be determined at compile time, it is a reference to the new object "kvill" created at runtime. Since s2 has the second half of newString("ill"), it is also It cannot be determined at compile time, so it is also an application of a newly created object "kvill"; if you understand this, you will know why this result is obtained.
4. String.intern():
Let me add one more point: exists in. The constant pool in the class file is loaded by the JVM during 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 there is a string constant with the same Unicode in the constant pool. If so, it returns its reference. If If not, add a Unicode string equal to str in the constant pool and return its reference; it will be clear by looking at Example 3. Example 3:
Copy the code code as follows:
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 );
The result is:
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 dispel another misunderstanding:
Someone said, "Use the String.intern() method to save a String class to a global String table. If a Unicode string with the same value is already in this table, then this method returns the string already in the table. address, if there is no string with the same value in the table, register your own address in the table. "If I understand the global String table he said as a constant pool, his last sentence, "If in If there is no string with the same value in the table, it is wrong to register your own address in the table:
Look at example 4:
Copy the code code as follows:
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() );
The result is:
false
kvill kvill
true
In this class, we did not declare a "kvill" constant, so there was no "kvill" in the constant pool at the beginning. When we called s1.intern(), a new "kvill" constant was added to the constant pool. It turns out that The "kvill" that is not in the constant pool still exists, which means it is not "registering its own address in the constant pool".
s1==s1.intern() is false, indicating 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 ==:
For String, this is simply to compare whether the Unicode sequences of two strings are equivalent, and return true if they are equal; and == is to compare whether the addresses of the two strings are the same, that is, whether they are references to the same string.
6. About String is immutable
There is a lot to say about this. As long as you know that a String instance will not change once it is generated, for example: String str="kv"+"ill"+" "+"ans";
There are 4 string constants. First, "kv" and "ill" generate "kvill" which is stored in the memory. Then "kvill" is combined with " " to generate "kvill" and is stored in the memory. Finally, it is combined with "kvill ans" to generate "kvill ans". ; and assign the address of this string to str, because the "immutability" of String generates many temporary variables, which is why it is recommended to use StringBuffer, because StringBuffer is changeable.