1. Firstly String does not belong eight kinds of basic data types, is a String object.
Because object default value is null, so the default value of String is null, but it is a special kind of object, there are other features not object.
2. New String () and new String ( "") are affirms that a new empty string, an empty string is not null;
3. String str = "kvill";
String str = new String ( "kvill"); distinction:
Here, we not talk about dumplings, not discussing the stack, only a brief introduction of constant pool this simple concept.
Constant pool (constant pool) refers to the period in the compiler identified, compiled and saved in the. Class some of the figures in the document. It includes on the type, method, the constant interface, including 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);
Results:
True
True
First, we have to know that Java will ensure that a string constants, there is only one copy.
S0 as examples of s1 and the "kvill" are string constants, which are compiled in the period to be determined, so s0 for s1 == true, and "kv" and "ill" are also string constants, When a string connected by a number of string constants from, it definitely is a string constants, s2 in the same period to be analyzed compiler for a string constants, s2 is constant pool "kvill" a quote .
Therefore, we have reached a s0 == s1 == s2;
Using new String () to create a string is not constant, not at compile view to determining, new String () to create a string constants not Add to the pool, they have their own address space.
See Example 2:
String s0 = "kvill";
String s1 = new String ( "kvill");
String s2 = "kv" + new String (the "ill");
System.out.println (s0 == s1);
System.out.println (s0 == s2);
System.out.println (s1 == s2);
Results:
False
False
False
S0 or in the case of two constants pool "kvill" applications, s1 because the compiler can not determine, it is created when running the new object "kvill" quote, s2 because of the latter part of new String (the "ill") So in the compiler can determine, is a newly created object "kvill" applications; understand these will come to know why this result.
4. String.intern ():
Just add on one point: there was. Class constant pool in the document, in running period was JVM loading, and can be expanded. String the intern () method is the constant expansion of a pool; example when a String str call intern () method, the Java find whether there is a constant pool the same Unicode string constants, and, if so, then return to their citations, if not, in the constant increase in a pool Unicode equivalent str string and return to its use; see clearly on the 3 cases
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 () / / of the constant pool "kvill" quoted assign s2
System.out.println (s0 == s1);
System.out.println (s0 == s1.intern ());
System.out.println (s0 == s2);
Results:
False
**********
False / / Although the implementation of the s1.intern (), but it does not assign the return value of s1
True / / Note s1.intern () returns the constant pool "kvill" quote
True
Finally I would like to get rid of a wrong understanding:
Some people say that the "use of String.intern () method can be a String class will be stored in a global String table, if the same value of the Unicode strings in the table, then the method has been returned to the table in a string address, if the table does not value of the same string, will be registered to the address of their own in the table "If I said this to the overall understanding of the String Table constant pool, his last sentence," If the table does not value of the same string, will be registered to the address of their own 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 category we do not have a reputation, "kvill" constant, constant pool is not a beginning, "kvill", as we call s1.intern () in the constant pool after adding a new "kvill" constant, the original is not constant pool "kvill" still exists, it is not "will be registered to the address of their constant pool."
S1 == s1.intern () for false that the original "kvill" still exists;
S2 is now constant pool "kvill" address, it is s1.intern == s2 () is true.
5. On the equals () and ==:
In simple terms this is the String comparison of the two Unicode string is a sequence, the same return if true, and the two string == is relatively the same as the address, which is the same whether a string references.
6. On String is not changed
Many have said that this, as long as we know that once generated String examples no longer change, for example: String str = "kv" + "ill" + "" + "ans";
Is a four string constants, first of all, "kv" and "ill" Generation "kvill" exist memory, and then "kvill" and also "and" Generation "kvill" exist in memory, and finally generate a "kvill ans" , and the address of this string assign a str, it is because the String of the "no change" has created a lot of temporary variables, which is why the reasons for the proposed use StringBuffer, because StringBuffer can change. |