The Java runtime environment has a string pool and maintained by String class. When performing statement string str = "abc", first check whether there is a string "ABC" in the string pool. If there is, "ABC" is directly assigned to the STR. If there is no existence, you will first create a string in the string pool. "ABC", then assign it to the STR. When executing statement string str = new string ("abc"), regardless of whether a string "ABC" exists in the string pool, create a string "ABC" directly (Note: New string "ABC" is not in the string pool Middle), then pay it to the STR. The previous statement is high, and the efficiency of the latter statement is low because the new string takes up memory space. String str = new string () creates an empty string, the same as the string str = new string (""). The following example shows: public class compareString {public static void main (string [] args) {string a = new string (); string aa = ""; string aaa = new string (""); string b = new string ("ASDF"); string c = new string ("asdf"); string d = "asdf"; system.out.println (a == aa); system.out.println (a == aaa); system. Out.println (a.intern () == aa.intern ()); system.out.println (a.intern () == aaa.intern ()); system.out.println (d == "asdf" ); System.out.println (b == c); system.out.println (b == d); system.out.println (B.Equals (c)); system.out.println (B.Equals d)); b = B.intern (); system.out.println (b == c); system.out.println (b == d); c = c.intern (); system.out.println B == C);}} The resulting result of the above programs is: falsefalsetruetruetruefalsefalsetruetruefalsetruetrue can verify the contents described earlier from the runtime. If you don't understand the INtern () method of the String class, you can refer to the document with the JDK: public string infurb1 () returns a canonical representation for the string Object. A pool of strings, initially empty, is maintained privately by the class string.
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals (Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a Reference to this string Object is returned.it Follows That for Any Two Strings S and t, sintern () == T.inTern () is true if and only if s.equals (t) is true.
All literal strings and string-valued constant expressions are interned String literals are defined in §3.10.5 of the Java Language Specification Returns:. A string that has the same contents as this string, but is guaranteed to be from a pool of unique strings From the CompareString class, we can also see that the difference from Equals (): 即 == Compare the reference (ie, the memory address) of the two objects, and equals () compares two objects. The value (ie the value stored in the memory address) is equal. Of course, Equals () has been overwritten in the individual class. (The above is only summarized for personal learning, please refer to you!)