In a nutshell:
If both sides of the operation are object handles, compare whether the two handles point to the same object. If both sides are basic types, then the value is compared.
= = does not belong to any class, and equals is a method of any class (in Java).
Equals () is the inherent method of each object, because the final base class of all classes is object (except the object itself), and equals () is one of the methods of object.
We can compare this:
1) Primitive1 (basic type) = = Primitive2 (basic type)
2) Object1 Reference1 (object reference) = = Object1 Reference2 (object reference)
3)Object1 Reference1 (object reference). Equals (Object1 Reference2 (object reference))
But it can't be compared:
Primitive1 (base type). Equals (Primitive2 (basic type))
And then look at the specific comparisons below.
values for the base data type:
int a = 4; int b = 4; System.out.println (a==b); True
Values for the base data type can only be compared using the "= =" comparison operator.
Comparison of values of reference types
Test T1 = new test (); Test t2 = new test (); System.out.println (T1==T2); Fasle System.out.println (t1.equals (T2)); False
The result of both sentences is false.
Reason:
The first sentence: objects created with the new operator will allocate a new memory address in memory, so the memory address of the two objects is different, Fasle.
The second sentence: must be clear equals exactly what is the object of what ~ ~
Take a look at the Equals method of the object class source code:
public boolean equals (Object obj) { return (this = = obj); }
The internal use is also = = comparison, so equals and = = In the object comparison is the same.
What's the use of equals?
Each object has its own memory, but each object has its own properties.
For example, both horses and cows have legs, and we want to determine whether the horses and cows are the same object by the number of legs.
At this point, the use of the Equals method in the object class will not satisfy the requirements. It is necessary to build two objects by overriding equals
The more identical specific content.
Examples are as follows:
Horse class horse{string type;int legs;//equal to the number of legs equal to public boolean equals (Object o) {if (this). legs== ((cattle) o). Legs) {return true;} return false;} Public Horse (String type,int legs) {this. Type=type;this. Legs=legs;}} The cattle class cattle{string Type;int legs;//equal to the number of legs equal to public cattle (String type,int Legs) {this. Type=type;this. Legs=legs;} public boolean equals (Object o) {if (this. legs== ((Horse) o). Legs) {return true;} return false;}} public class equalstest{public static void Main (string[] args) { cattle c = new cattle ("I am The Cattle", 4); Horse h = new Horse ("I am The Horse", 4); if (C.equals (h)) {System.out.println (c.type); System.out.println (H.type); System.out.println ("Cattle Equals Horse");}}}
The final result:
"I am The Cattle"
"I am The Horse"
"Cattle Equals Horse"
by using the Equals method to equal the things that are not equal to the wind horse cows, we can see that the object is actually equal, depending on the user's needs
Please. We define what kind of standard, the computer will be steadfast for us to achieve.
Therefore, when using Java classes to describe objects, if you want to determine whether the object is the same, it will usually overwrite the Equals method, set up according to the
The same basis as the characteristics of the comparison.
Beginner Java (i)--equals and "= =" Difference