All classes inherit the Equals method from the object class, and the source code for the Equals method in the object class is as follows:
public boolean equals(Object obj)
{
return this == obj;
}
The Equals method in object is to directly determine whether the value of this and obj itself is equal, that is, to determine whether the object referenced by the calling equals and the parameter obj is the same object, so-called the same object refers to the same piece of memory in the same storage unit, Returns true if this and obj point to the same piece of memory object, returns False if this and obj point to not the same block of memory, note that false is returned even if the content is exactly equal to two different memory objects.
So what is the difference between the Equals () method and the previous "= ="?
Understanding the main partitioning of Java memory before presenting the demo code can help us to better understand the examples shown below.
The heap area in the Java Virtual machine stores the new object, the stack holds the local variables, and the method area contains all the information of the class, including all methods, static variables, constants, and so on.
Let's take a look at the following example to deepen the equals and code as follows:
Public class  equals{
public  static  void  main ( string[] args) {
String s1= " Apple ";
String s2= "Apple";
System. out.println (S1==S2); //true
System. Out.println (S1. Equals (S2)); //equals compares the content, true
String s3= new string ( "Apple");
String s4= new string ( "Apple");
System. out.println (S3==S4); //false
System. Out.println (S3. Equals (S4)); //true
}
}
As can be seen from the above example, "= =" Compares two referenced objects for equality, while the Equals () method compares the actual contents of two objects. We understand this distinction by combining the memory partitioning above.
String str1=new String("apple");
String str2=new String("apple");
System.out.println(s3==s4); //false
System.out.println(s3.equals(s4)); //true
The above few lines of code memory analysis are as follows:
because "= =" compares the two referenced objects for equality, it is easy to see, so System.out.println (S3==S4), the result is false, and the Equals () method compares the actual contents of two objects, As you can see, S3 and S4 all point to Apple, the content is the same, so System.out.println (S3.equals (S4)), the result is true.
Look again
String s1="apple";
String s2="apple";
Memory analysis such as:
It is easy to see that the result of System.out.println (S1==S2) is true.
The Equals () method of the Java object class