1. First, string does not belong to 8 basic data types, and 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 with some attributes that other objects do not have.
2. The new string () and the new string ("") are all declarations of a newly empty string, which is not null;
3. String str= "Kvill";
String Str=new string ("Kvill"), the difference:
Here, we don't talk about heaps, we don't talk about stacks, we 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, as well as 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 results are:
true
true
First of all, we need to know that the result is Tao Java will ensure that a string constant has only one copy.
Because the "Kvill" in the 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 string constants, and when a string is concatenated by multiple string constants, it is definitely a string constant, So S2 is also parsed as a string constant at compile time, so S2 is also a reference to "Kvill" in a constant pool.
So we come to s0==s1==s2;
A string created with new string () is not a constant and cannot be determined at compile time, so the string created by new string () is not placed in a constant pool and has its 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 results are:
false
false
false
Example 2, S0 or the application of "Kvill" in a constant pool, S1 is a reference to the new object "Kvill" that was created at run time because it cannot be determined at compile time, S2 is also a newly created object because it has the latter half of new String ("ill") and cannot be determined at compile time. Kvill "The application of the", understand these also know why to come to this result.