Java data abstraction equals of 3 methods to be overloaded ()

Source: Internet
Author: User

Boolean Equals (Object x);

This method and the operator "= =" are very different, equals the value of the implementation of the object is logically equal to the judgment, that is, "= =" is simply to determine whether the object's reference is the same.

The equivalence of equals requires the following relationships: reflexive, symmetric, transitive, non-void, and consistent.

Package Equalstesting;public class Person {private string name;private int age;public person (String Name,int age) {This.na me = Name;this.age = age;} public boolean equals (Object x) {if (this = = x) return true;if (x = = null) return False;if (This.getclass ()! = X.getclass ()) re Turn false; person is = (person) x;if (this.name! = that.name) return false;if (this.age! = that.age) return False;return true;} public static void Main (string[] args) {//TODO auto-generated method Stubperson p1 = new Person ("Jay", 30); person P2 = new Person ("Kim", 29); Person P3 = new Person ("Jay", 30); System.out.println (P1.equals (p2)); System.out.println (P2.equals (p1)); System.out.println (P1.equals (p3)); System.out.println (P3.equals (p1)); SYSTEM.OUT.PRINTLN (P1.equals (null));}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java data abstraction equals of 3 methods to be overloaded ()

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.