Although the question about Java strings "==" and "equals" is the most basic question in Java learning, there are still many students who have just learned Java who are not clear about the principles. I recently discussed this issue with my friends. , so I wrote an article to share with you my own understanding.
First let's look at an example:
Copy the 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 don’t have the answer yet, let’s try to do it. Can you guarantee that everything is correct?
The answer is announced below:
Copy the 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
To understand this kind of problem well, the best way is to have a deep understanding of the mechanism and principles of the "==" method and "equals" method of String in Java. Everyone must know that "equals" is to compare the contents of strings. Since all the string contents in the above program are "test", they will all be equal when compared with equals.
But do you know that the equals method and the "==" method of the base class Object in Java are actually the same? It's just that after the String class inherits the Object class (all classes in Java inherit the Object class), it overloads the equal method, making it a comparison of the contents of the string.
After having a good understanding of equals, let us now study the intricacies of the "==" method.
"==" is an operator in Java. The content it compares is the pointers of two objects, which is the address of the actual object. So it is easy to understand that e==a returns true.
Let's look at the comparison between c and d again. When we see the new keyword, it means that both c and d have reapplied for a memory address, and then assigned the value "test", so c==d returns false. It’s understandable in the same way
The "==" operation between a, c, d, and f all returns false.
Then let’s look at g= a + “”. Although we don’t see the new keyword, since the String class “+” operator is overloaded, a new String will definitely be added in the overloaded method, so it becomes became the above situation. Then the most difficult thing to understand is the comparison between a and b. In fact, this is the result of the optimization of the Java compiler. The Java compiler has a storage area called the string constant pool. Since String a = "test" After the statement is compiled, "test" will be stored in this string constant pool. Then when b is defined again, b still points to this area, so a and b still point to the same area. So a==b returns true.