Idea: clarify one thing separately and then clarify the other. In this way, the differences will naturally come out.
(1)= OperatorDedicated for comparisonTwo VariablesIs the value equal, that is, used to compare whether the values stored in the memory corresponding to the variable are the same. To compare two basic types of data or whether two referenced variables are equal, you can only use the = Operator.
If the data that a variable points to is of the object type, two memory blocks are involved. The object occupies one memory (heap memory) and the variable also occupies one memory, for example, objet OBJ = new object (); the variable obj is one memory, and newobject () is another memory, the value stored in the memory corresponding to the variable obj is the first address of the memory occupied by the object. For variables pointing to the object type, if you want to compare whether two variables point to the same object, it depends on whether the values in the memory corresponding to the two variables are equal, in this case, we need to use the = Operator for comparison.
(2)Equals MethodIs used to compare twoIndependent objectWhether the content is the same is like comparing whether two people have the same looks. The two objects it compares are independent.
For example, for the following code:
String A = new string ("foo ");
String B = new string ("foo ");
Two new statements create two objects, and then use the variables A and B to point to one of them. These two variables are two different objects whose first addresses are different, that is, the values stored in expression A and expression B are different. Therefore, expression A = B returns false, and the content of the two objects is the same. Therefore, expression. equals (B) returns true.
(3) In actual development, we often need to compare the passed string content, for example, string input = ...; Input. equals ("Quit"), many people do not pay attention to it and use = for comparison. This is wrong. You can find some practical teaching videos from the Internet, there are a lot of such errors. Remember, strings are basically compared using the equals method.
If a class does not have its own equals method, It inherits the equals method of the object class and the equals method of the object class.
The implementation code of the method is as follows:
Boolean equals (Object O ){
Return this = O;
}
This indicates that if a class does not define its own equals method, its default equals method (inherited from the object class) will
The = Operator is used to compare whether the objects pointed to by two variables are the same object. In this case, equals and
= Returns the same result. If two independent objects are compared, false is always returned. If you want to write a class
If the content of the two instance objects created in this class is the same, you must overwrite the equals method and write the code yourself.
To determine the situation in which the content of the two objects is the same.