"= =" compares the memory addresses of two objects, while the Equals method compares the memory addresses of two objects by default.
1.String str = "Hello" generated string, first of all to check the string constant pool in the existence of the "hello", if present, will not create a new "Hello", directly return to the constant pool of this string address, if not exist, Then add this string to the string constant pool. A string constant pool that exists in the method area.
2.String str = new string ("Hello"), first of all in the constant pool looking for the existence of this string, the existence is no longer established, if not exist, then set up, and then copy to the heap memory! In heap memory new A string, of course, if the new string, Then their memory addresses are different. But for new strings, they are the same when using equals, because the method of overriding equals in string is to compare each char with a string to a char array, and finally to go true or False. The new String creates two of this object altogether. One in the constant pool, one in heap memory!
3. String comparison tips:
Use "China". Equals (str), with the constant equals, to prevent null pointer exceptions from occurring. The method call is not empty.
Java string comparison