Example I:
String1 = "AAA ";
String2 = "AAA ";
String string3 = new string ("AAA ");
String string4 = new string ("AAA ");
String1 = string2 // true;.
String1.equals (string2); // true;
String3 = string4; // falseBecause two objects are created with new, they are two different memory addresses.
String3.equals (string4); // trueThe string class cannot be changed, so it will point to the same memory address, so the return value is true.
Equals () is the object method, so it is only suitable for objects and not for basic types. Equals () compares the memory addresses of two objects with "=" by default, if you want to compare the content of two objects, you must override the equals () method... and = can compare two basic types, or can be objects...
String equals () method Rewriting:
Public Boolean equals (Object object ){
If (this = anobject)
{Return true ;}
If (anobject instancdof string)
{String anotherstring = (string) anobject;
Int n = count;
If (n = anotherstring. Count)
{Char V1 [] = value;
Char V2 [] = anotherstring. value;
Int I = offset;
Int J = anotherstring. offset;
While (n --! = 0)
{If (V1 [I ++]! = V2 [J ++]
Return false ;}
Return true ;}
}
Return false;
}
All in all: in a class object, the equals () method compares the object Value and = compares the object. that is, the object reference (that is, the memory address). In some special cases, equals () overwrites the method ..
Equal: used to compare whether the content inside two objects is equal, because all classes are inherited
From the java. Lang. object class, so if this method is not overwritten, call
Is still the method in the object class, while the Equal method in the object returns =
Therefore, if the method is not overwritten
Any meaning.
=: Used to determine whether the addresses of the two objects are the same, that is, whether the addresses of the two objects are the same. Compared
Is a true pointer operation.
1. Declaration format
Public Boolean equals (Object OBJ)
The comparison rule is: if the object referenced by the OBJ parameter is the same as the current object, true is returned; otherwise, false is returned.
For example, the following two objects animal1 and animal reference different objects, so the result of comparison with = or equals () is false, while the animal1 and animal variables reference the same dog object, therefore, the result of comparison with = or equals () is true.
Animal animal1 = new dog ();
Animal animal = new CAT ();
Animal anim_3 = animal1;
Then animal1 = animal (false)
Animal1.equals (animal) (false)
Animal1 = anim_3 (true)
Animal1.equals (anim_3) (true)
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. 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; otherwise, false is returned.
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.