Although the question about Java strings "=" and "equals" is the most basic problem in Java learning, many students who have just learned Java cannot figure out the principles, I recently discussed this issue with my friends, so I wrote an article to share my understanding with you.
First, let's look at an example:
Copy codeThe Code is 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 =;
String f = new String ();
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 the question first, can you make sure it is all correct?
The answer is as follows:Copy codeThe Code is 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
To better understand this question, the best way is to thoroughly understand the mechanism and principle of the String "=" and "equals" methods in Java. "Equals" must be known as comparing strings. Since all the strings in the above program are "test", equals is used to compare them.
But do you know that the equals method of the basic Obejct class in Java is actually the same as the "=" method? Only after the String class inherits the Object class (all classes in Java inherit the Object class), the equal method is overloaded to make it a comparison of the content in the String.
After a good understanding of equals, let's take a look at the confusing "=" method.
"=" Is an operator in Java. It compares two object pointers, that is, the actual object address. Therefore, it is easy to understand that e = a returns true.
Let's take a look at the comparison between c and d. When we see the new Keyword, it means that c and d have re-applied for a memory address and assigned the value "test" to it ", therefore, c = d returns false. Likewise, we can understand
The "=" operation between a, c, d, and f returns false.
Then let's take a look at g = a + "". Although we didn't see the new Keyword, but because the String class "+" operator is overloaded, the overloaded method will certainly have a new String, so it turns into the above situation. The most difficult thing to understand is the comparison between a and B. In fact, this is the result of Java compiler optimization. the java compiler has a storage area called the String constant pool, after the String a = "test" statement is compiled, "test" is stored in this String constant pool. When B is defined, B still points to this region, therefore, a and B point to the same region. So a = B returns true.