First of all, a string does not belong to eight basic data types, and it is an object.
Because the default value of object is null, the default value of String is also null; But it is a special object with some characteristics that other objects do not have.
2.
Both new String () and new String ("") declare a new empty string, which is not null;;
3.
String str = " kvill
line
Str = new string ("kvill"); The difference is:
Let's not talk about heaps or stacks here, but simply introduce the simple concept of constant pool.
Constant pool (constant
Pool) refers to the. Class file. It includes constants about classes, methods, interfaces, etc. , and string constants.
See example 1:
line
s0 = " kvill
line
s 1 = " kvill ";
line
S2 = "kV"+"Sick";
System.out.println(
s0 = = s 1);
System.out.println(
s0 = = S2);
The results are as follows:
real
real
First, we need to know that Java will ensure that there is only one copy of a string constant.
S0 = = s 1 is true; Because S0 and "kvill" in S 1 are string constants, they are determined at compile time. And "kv" and "ill" are also string constants. When a string is connected by multiple string constants, it must be a string constant, so s2 is also parsed into a string constant at compile time, so s2 is also in the constant pool.
A reference to "kvill".
So we get S0 = = s1= = S2;
Use a new one
The strings created by string () are not constants and cannot be determined at compile time, so the strings created by new String () are not put into the constant pool, and they have their own address space.
See example 2:
line
s0 = " kvill
line
S 1 = new string ("kvill");
line
S2="kv"+new string ("ill");
System.out.println(
s0 = = s 1);
System.out.println(
s0 = = S2);
System.out.println(
s 1 = = S2);
The results are as follows:
wrong
wrong
wrong
In Example 2, s0 is still the application of "kvill" in the constant pool, and s 1 is a reference to the new object "kvill" created at runtime, because it cannot be determined at compile time, and s2 has the second half of new.
String(“ill ") cannot be determined at compile time, so it is also an application of a newly created object" kvill "; Knowing this, you will know why this result has been reached.
4.
String.intern():
One more thing: constant pool in. Class files are loaded by the JVM at runtime and can be extended. String's intern () method is a method to extend the constant pool. When a string instance str calls the intern () method, Java looks up whether there is a string constant with the same Unicode in the constant pool, and returns its reference if there is one; If not, add a string with Unicode equal to str to the constant pool and return its reference; For clarity, see Example 3.
Example 3:
line
s0 = " kvill
line
S 1 = new string ("kvill");
line
S2 = new string ("kvill");
System.out.println(
s0 = = s 1);
System.out.println(
"**********" );
s 1 . intern();
S2 = S2 . intern();
//Assign a reference to "kvill" in the constant pool to s2.
System.out.println(
s0 = = s 1);
System.out.println(
s0 = = s 1 . intern());
System.out.println(
s0 = = S2);
The results are as follows:
wrong
**********
wrong
//Although s 1.intern () was executed, its return value was not assigned to s 1.
real
//Description: s1.inter () returns the reference to "kvill" in the constant pool.
real
Finally, I want to break a wrong understanding:
Some people say, "You can save a string class to the global string table by using the string. int () method. If a Unicode string with the same value already exists in the table, this method returns the address of the existing string in the table. If there is no string with the same value in the table, register your address in the table. " He said, if I put this global string,
If the string table is understood as a constant pool, his last sentence "If there are no strings with the same value in the table, register your address in the table" is wrong:
See example 4:
line
S 1 = new string ("kvill");
line
S2 = s 1 . intern();
System.out.println(
s 1 = = s 1 . intern());
System.out.println(
s 1+" "+S2);
System.out.println(
S2 = = s 1 . intern();
Results:
wrong
Kewell
Kewell
real
We didn't name a "kvill" constant in this class, so there was no "kvill" in the constant pool at first. When we called s 1.intern (), we added a new "kvill" constant to the constant pool. The "kvill" that was not in the constant pool still exists, which is not "registering your address in the constant pool".
S 1==s 1.intern () is false, indicating that the original "kvill" still exists;
S2 is now the address of "kvill" in the constant pool, so s2==s 1.intern () is true.
5.
Oneequals () and = =:
For String, this is simply to compare whether the Unicode sequences of two strings are equivalent, and if they are equal, it will return true 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 strings being immutable.
There is a lot to say about this, as long as you know that once an instance of String is generated, it will not change, such as: String.
str = " kv "+" ill "+" "+" ans ";
There are four string constants. First, "kv" and "ill" generate "kvill", then "kvill" and "
Generate "kvill" in memory, and finally use.
ans”; And assign the address of this String to str, because the "invariance" of String produces many temporary variables, which is why it is suggested to use StringBuffer, because StringBuffer is changeable.