Equals () in Java ()

Source: Internet
Author: User

After several days of study, I finally had a comprehensive understanding of equals usage and made a summary.

1. What is the meaning of the equals () method defined in the object.

(The = function is attached here, which is still described later. Because = is closely related to the equals () method, I have my own opinions later.

= It has different functions for comparing and referencing basic data types:
Compares the basic data types. If the two values are the same, the result is true.
When comparing references, if the reference points to the same object in the memory, the result is true)

 

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.

Public class fish {
Private int weight;
Private string color;

 

Public fish (INT weight, string color ){
This. Color = color;
This. Weight = weight;
}

Public int getweight (){
Return weight;
}

Public void setweight (INT weight ){
This. Weight = weight;
}

 

Public String getcolor (){
Return color;
}

Public void setcolor (string color ){
This. Color = color;
}

}

Public class initialize stest {
Public static void main (string [] ARGs ){
Fish F1 = new fish (1, "blue ");
Fish F2 = new fish (1, "blue ");

 

System. Out. println (F1 = F2 );
System. Out. println (f1.equals (F2 ));
}
}

------ The running result is ------

False
False

It can be seen that the meaning of the equals () method is to determine whether the references of the two objects are the same.

2. 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)

Public class initialize stest {
Public static void main (string [] ARGs ){
String S1 = new string ("Sss ");
String S2 = new string ("Sss ");

System. Out. println (S1 = S2 );
System. Out. println (s1.equals (S2 ));
}
}

-------- The running result is ------

False
True

From this we know that the equals () method in string is overwritten, so that its meaning becomes to compare whether the content of the two objects is consistent.

3. if we want to overwrite the equals () method by ourselves and compare the content or use other methods (of course, we generally use this method to compare the content, but what is the real implementation in the covered method? Who knows ~)

Public class fish {
Private int weight;
Private string color;

 

Public fish (INT weight, string color ){
This. Color = color;
This. Weight = weight;
}

@ Override
Public int hashcode (){
Final int prime = 31;
Int result = 1;
Result = prime * result + (color = NULL )? 0: color. hashcode ());
Result = prime * result + weight;
Return result;
}

@ Override
Public Boolean equals (Object OBJ ){
If (this = OBJ)
Return true;
If (OBJ = NULL)
Return false;
If (getclass ()! = Obj. getclass ())
Return false;
Fish Other = (fish) OBJ;
If (color = NULL ){
If (other. color! = NULL)
Return false;
} Else if (! Color. Equals (other. Color ))
Return false;
If (weight! = Other. Weight)
Return false;
Return true;
}
Public int getweight (){
Return weight;
}

Public void setweight (INT weight ){
This. Weight = weight;
}

Public String getcolor (){
Return color;
}

Public void setcolor (string color ){
This. Color = color;
}

}

Public class initialize stest {
Public static void main (string [] ARGs ){
Fish F1 = new fish (1, "blue ");
Fish F2 = new fish (1, "blue ");

 

System. Out. println (F1 = F2 );
System. Out. println (f1.equals (F2 ));
}
}

------ The running result is ------

False
True

In this example, the equals () method and the hashcode () method are rewritten to make the equals () method different from the original meaning. It is no longer to compare whether the references of the two objects are the same, instead, compare whether the content is the same.

The above content is a summary in the general book. Below are some things I have come up.

We can know that in the final analysis, the computer only performs the sum or non-operation of binary numbers and addition multiplication. With some basic operators, all functions are implemented by using basic operators in essence. = Is one of the basic operators. It has different functions for comparing and referencing basic data types:
Compares the basic data types. If the two values are the same, the result is true.
When comparing references, if the reference points to the same object in the memory, the result is true.

As for equals (), we can infer that the implementation of equals () must use the = Operator. Further Thinking, equals () does not mean the role of = Operator in object comparison. So, since the two have the same effect, why do we need to develop an equals () method. Because the = operator does not allow us to overwrite, that is, it limits our expression. In the third example above, we repeat the equals () method to compare whether the object content is the same. However, the = Operator cannot be used.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.