Although the problem with the Java string "= =" and "equals" is one of the most fundamental problems in Java learning, but there are still a lot of students just learning Java to understand the principle of the inside, and friends recently discussed this issue, so write an article to share my own understanding.
First let's look at an example:
Copy Code code as follows:
public class Teststringequals {
public static void Main (string[] args) {
String a = "test";
String b = "Test";
String c = new string ("test");
String d = new string ("test");
String e = A;
String f = new String (a);
String g = a + "";
System.out.println (A = = B?) ' Expression \ ' a==b\ ' is true '
: "Expression \" A==b\ "is false");
System.out.println (A = = c?) ' Expression \ ' a==c\ ' is true '
: "Expression \" A==c\ "is false");
System.out.println (c = d?) ' Expression \ ' c==d\ ' is true '
: "Expression \" c==d\ "is false");
System.out.println (A = = e?) ' Expression \ ' a==e\ ' is true '
: "Expression \" A==e\ "is false");
System.out.println (A = = f?) ' Expression \ ' a==f\ ' is true '
: "Expression \" a==f\ "is false");
System.out.println (A = = g? ' Expression \ ' a==g\ ' is true '
: "Expression \" A==g\ "is false");
if (a.equals (b) && b.equals (c) && c.equals (d) && d.equals (E)
&& e.equals (f) && f.equals (g)) {
System.out
. println ("A equals B equals C equals D equals E equals F equals g");
}
}
}
If you do not answer first, we try to do it, can we guarantee all right?
Here's the answer:
Copy Code code as follows:
Expression ' a==b ' is true
Expression "A==c" is false
Expression "C==d" is false
Expression ' a==e ' is true
Expression "A==f" is false
Expression "A==g" is false
<div style= "Text-align:left;" ></div>a equals B equals C equals D equals E equals F equals g
The best way to understand this problem is to understand the mechanism and principle of the "= =" method and the "equals" method of String in Java. "Equals" Presumably everyone knows, is to compare the content of strings, because all the strings in the above program is "test", so compare them with equals.
But do you know that the Equals method of the base class OBEJCT in Java is consistent with the "= =" Method? Just as the string class inherits the object class (all classes in Java inherit the object class), the equal method is overloaded so that it becomes content in the comparison string.
After a good understanding of equals, now let's look at the "= =" method of the bewildering.
"= =" is an operator in Java that compares the contents of a pointer to a two object, which is the address of the actual object. It is therefore easy to understand that E==a returns TRUE.
Let's take a look at the comparison between C and D, see the New keyword, the C and D are all reapply a memory address, and then the inside assignment is "test", so C==d return false. The same can be understood.
the "= =" Operation between A,c,d,f returns FALSE.
Then look at g= A + "", although not see the new keyword, but because the string class "+" operator is overloaded, the overloaded method will certainly be a new string, so it becomes the case above. So the hardest thing to understand is the comparison of A and B, in fact, this is the result of the Java compiler's optimizations, the Java compiler has a storage area called a string constant pool, and since string a = "test" is compiled, "test" is stored in this string constant pool, Then b still points to the area, so that A and B are still pointing to the same area. So the A==b returns True.