About the Java override Equals method

Source: Internet
Author: User

First, why would you rewrite the Equals method?

This may be a problem that we are more concerned about.

My personal understanding is that the Equals method that comes with the Java object class can often be less than adequate.

So we need to rewrite him, give him some custom features or take it to a specific place.

Let's say we want to compare whether the two objects are the same. We're going to take the same properties in these 2 objects.

However, the methods provided in the object class do not provide such functionality, so we need to rewrite them at this time.

Let's take a look at the example (with some of my own understanding, only for reference, welcome to criticize correct.)

1  Public classEqualsoverride {2     3      Public Static voidMain (string[] args) {4person P1 =NewPerson (11, "Xiaoming");5person P2 =NewPerson (11, "Xiaoming");6         BooleanFlag =p1.equals (p2);7         if(flag) {8System.out.println ("P1 and P2 equal");9}ElseSystem.out.println ("P1 and P2 are not equal");Ten     } One      A } - classPerson { -     intAge ; the String name; -      PublicPerson (intage,string name) { -          This. Age =Age ; -          This. Name =name; +     } -      Public intGetage () { +         returnAge ; A     } at      Public voidSetage (intAge ) { -          This. Age =Age ; -     } -      PublicString GetName () { -         returnname; -     } in      Public voidsetName (String name) { -          This. Name =name; to     } +  - @Override the      Public Booleanequals (Object obj) { *         if( This= = obj) {//if the same object $             return true;Panax Notoginseng         } -         if(obj = =NULL) {//If the passed argument is empty, it returns NULL because the object is not meaningful compared to an empty object. the             return false; +         } A         if(objinstanceofperson) {//if the reference type of the parameter belongs to the person class thePerson p =(person) obj; + //as we go down, we know that polymorphism is premised on the reference of the parent class to the object of the subclass, and once polymorphic, the properties of subclasses and the methods peculiar to subclasses are temporarily masked. - //so if we want to access the properties of the subclass, we need to move down. Because object is the parent of all classes, the reference to the parameter passed to obj is also a reference to the class objece, which is just the object that points to the person $ //. And then we compare 2 objects, actually need to compare the properties of these 2 objects, just like you compare two people, will compare their age name height and so on. $             if( This. Age = = P.age && This. Name.equals (P.name)) { - //This here represents the current object, which refers to the P1 in P1.equals (P2), which is passed to obj as a parameter. - //because age we define the integer type, so the direct = = Judgment, name is a string type, because the string type comparison, can only compare the content, can not be used with the ==,string type = = To compare, is the comparison of memory the //address value, not a content value, and then there is an overridden Equals method in string, so we can call it directly and then compare it to the name of another object. Then the short-circuit and the condition is that both sides are - //True to return true.Wuyi                 return true; the             } -}return false; Wu     } -}

About the Java override Equals method

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.