Replication Equals method in Java

Source: Internet
Author: User

Generally, all classes should replicate the Equals method in object. The steps are roughly three-step:

1) Determine whether the address of two objects is consistent

2) Determine if the second object is the same subclass instance

3) Determine whether the content is consistent

classperson{ Public intAge ;  PublicString name;  PublicPerson (intAge , String name) {         This. Age =Age ;  This. Name =name; }     Public Booleanequals (Object obj) {//The first step is to determine whether the two object addresses are consistent        if( This==obj)return true; //determines whether the second object is the same subclass instance        if(! (objinstanceofPerson )) return false; //Judging whether the content is consistentPerson Anotherone =(person) obj; if( This. Name = =NULL&& Anotherone.name! =NULL)            return false; if( This. Name.equals (Anotherone.name) && This. Age = =anotherone.age)return true; return false; }} Public classhello{ Public Static voidMain (string[] args) {person P1=NewPerson ("cherry")); Person P2=NewPerson ("cherry"));    System.out.println (P1.equals (p2)); }       }

Replication Equals method in Java

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.