1. Differences between the equals method and "=" in Java:
The equals method is a Java. Lang. Object Class method.
There are two usage instructions:
(1) For string variables, the comparison method is different when "=" and "equals ()" are used to compare strings.
"=" Compares the values of the two variables, that is, the first address of the two objects in the memory.
"Equals ()" compares whether the content in the string is the same.
(2) For non-string variables, the "=" and "equals" methods are used to compare their functions.
The first address of the object in heap memory, which is used to compare whether two referenced variables point to the same object.
= The comparison operator is also used to compare whether the object is the same, that is, the first address of the object in the memory. The address of a class object is the first address of the memory space contained in the class, which corresponds to the address of a specific member variable.
The equals method in the object class is used to compare the "Address"
Some classes in the JDK class overwrite the equals () method of the oject class. The comparison rule is: if the two objects have the same type and the content is consistent, true is returned. These classes include:
Java. Io. File, java. util. Date, java. Lang. String, packaging class (integer, double, etc)
For example
Integer int1 = new INTEGER (1 );
Integer int2 = new INTEGER (1 );
String str1 = new string ("hello ");
String str2 = new string ("hello ");
Int1 = int2 output: false, because different objects
Int1.equals (int2) Output: True
Str1 = str2 (false)
Str1.equals (str2) (true)
Of course, you can customize the equals () method that overwrites the object class and redefine the comparison rules.
If: stringbuffer S1 = new stringbuffer ("");
Stringbuffer S2 = new stringbuffer ("");
Result: s1.equals (S2) // false
Explanation: The stringbuffer class does not redefine the equals method. Therefore, this method comes from the object class,
The equals method in the object class is used to compare "addresses", so it is equal to false.
For example, the following equals () Comparison rule for the person class is as follows: if both objects are person classes and their attribute names are the same, the comparison result is true, no
Returns false.
Public class person {
Private string name;
Public Person (string name)
{
This. Name = Name;
}
Public Boolean equals (Object O)
{
If (this = 0) return true;
If (! O instanceof person) return false;
Final Person Other = (person) O;
If (this. Name (). Equals (other. Name ()))
Return true;
Else
Return false;
}
}
Note: When rewriting the equals method, you must satisfy the Discrete Mathematical characteristics.
1. Self-inverse: the return value of X and X. Equals (x) must be true.
2 symmetry: For any referenced values X and Y, if and only when Y. Equals (x) returns true, the return value of X. Equals (y) must be true;
3 pass: If X. Equals (y) = true, Y. Equals (z) = true, X. Equals (z) = true
4. Consistency: If the objects involved in the comparison are not changed, the results of the object comparison should not be changed.
5. non-null: the return value of any non-null reference values X and X. Equals (null) must be false.