Equals exploration
-------- Java Reading Notes (1)
Section 1: EqAuls and = =
1) Comparison mode angle:
= = Is the process-oriented operator; Equals is an object-oriented operator
= = Do not belong to any class, Equals is a method of any class (in Java);
We can 1) Primitive1 (basic type) = = primitive2 (basic type);
2) Object Reference1 (object reference) = = Object Reference2 (object reference)
3) Object Reference1 (object reference) .Equals (object reference2)
These three comparisons
However, it cannot be primitive1 (basic type) .Equals (primitive2);
For basic types, there is no message to send a message or not, there will be no
Method member.
2) Comparative purpose perspective:
1) If you want to compare whether the two basic types are equal, please use = =;
2) If you want to compare if the two object references are equal, please use = =;
3) If two objects (logical) are compared, please use Equals;
Section 2: Interpretation of two objects (logical):
Some people will ask: In C , compare two objects, etc., can you use ==? I know that you mean the operator overload, but unfortunately, operator overload is not supported in Java (there is also an overloaded operator in Java, they are " ", " =", but only two, but also built-in Implemented); Therefore, if the object is equal, this responsibility is implemented by Equals ().
This "logical" actually depends on the views of human beings. In actual development, it depends on the needs of users;
Some people will see: "Depending on the view of human beings" is too broad and not serious, if someone wants two pieces
Does the things that the wind is not phase, whether Equals can make such a comparison? We can say
The following example illustrates this:
Class Horse {
String Type;
INT LEGS;
/ / Equal standard: the number of legs, etc.
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.LES.LEGS = LEGS;
}
}
Class Cattle
{
String Type;
INT LEGS;
/ / Equal standard: the number of legs, etc.
Public cattle (String Type, INT LEGS) {
THIS.TYPE = TYPE;
THIS.LES.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'm the Cattle", 4);
Horse H = New Horse ("I'm the Horse", 4);
IF (c.equals (h)) {
System.out.println (c.Type);
System.out.println (H.Type);
System.out.println ("Cattle Equals Horse");
}
}
}
Output: "I'm the cattle"
"I'm the horse"
"Cattle Equals Horse"
You look: Niu Chey is equivalent to the horse, why? Because our definition is: the number of legs, etc., you will say: "This is too funny", is funny, but this is the view of human beings, the computer can not be funny concept, of course, there is no "not funny" concept, We define what is the same standard, he is implemented for us;
So: Equal standard (ie demand) must be set, otherwise, the funny thing can be more
Section III: Equals () origin:
Equals () is an innate approach because all the final base classes of all classes are Object (remove Object itself); and equals () is one of the objects of Object.
We might want to observe the Source Code of equals () in Object:
Public Boolean Equals (Object Obj) {
Return (this == Obj);
}
Note "Return (this == Obj)"
THIS and OBJ are object references, not object itself. So the default implementation of equals () is comparison
Whether the object reference is consistent; why do you implement this? As we said: Whether the object is equal, it is determined by our demand, the world's class is strange (of course, these classes are created according to the simulation real world), although Object is their common ancestor, but he How can you know the standard of his child's grandchildren? But he understands that any object, you are always equal to yourself, what is "I am always equal to myself", how can I judge "I am always equal to myself?" A object has only one in memory, but his reference can have a number of endless, "object own reference 1 = object own reference 2", can you tell "you are always equal to yourself"? Therefore, the default implementation is naturally
"Return (this == Obj)";
And the class we wrote, the standard equivalent is established by us, so it is inevitable to cover
Inherited public boolean equals (object obj);
If you have experienced experience with Equals () (not tight, please think about a question:
"Does the two objects (logical) are actually more than? Yes, maybe you have dropped:
It is the comparison of objects' properties (ie, Field, or data members). The method is not comparable. (This question is not a little mentally hilar? Haha) Section 4: Thinking on a presentation
Introduction is as follows: One words are shielded: I want to compare whether the data is equal in the stack, please use = =;
If you want to compare the data in the stack, please use equals;
Because the (root) basic type, the (root) object reference is in the stack; the object itself is in the heap;
This sentence is not right, the problem is, it is "data" two words, first look at the stack, data or basic type, or reference to objects, == comparison is true; but in the heap? Is the object not a pile? Shouldn't you compare with Equals? However, we are more than "data" in the heap, there are objects in the heap, what makes the object? It may be an object reference, which may be a basic type, or both. If we want to compare them, what should I use, use "equals ()"? No, only "= ="! So the correct conclusion is: I want to compare whether the data is equal in the stack, please use = =; Whether the data in the pile is equal, please use equals;
Because the (root) basic type, the (root) object reference is in the stack (so-called "root", refers to the contained by any other objects; and the object itself is in the heap.