The Equals () method in Java

Source: Internet
Author: User

As we all know, in Java, for the comparison of objects, if "= =" is compared with the object reference, and equals is the content of the object to compare.

In general, when designing a class, we need to override the Equals method of the parent class, and when overriding this method, we need to design the following rules:

1. Reflexivity: The return value of X,x.equals (x) for any reference value must be true.
2. Symmetry: For any reference value x, Y, the return value of X.equals (Y) must be true if and only if the return value of Y.equals (x) is true;
3, transitivity: if X.equals (y) =true, y.equals (z) =true, then X.equals (z) =true
4, Consistency: If the object participating in the comparison has not changed, then the results of the object comparison should not have any change
5, non-nullability: any non-null reference value x,x.equals (NULL) The return value must be false

For example:

Java code
  1. Public class People {
  2. private String FirstName;
  3. private String LastName;
  4. private int age;
  5. Public String Getfirstname () {
  6. return firstName;
  7. }
  8. public void Setfirstname (String firstName) {
  9. this.firstname = firstName;
  10. }
  11. Public String Getlastname () {
  12. return lastName;
  13. }
  14. public void Setlastname (String lastName) {
  15. this.lastname = lastName;
  16. }
  17. public int getage () {
  18. return age;
  19. }
  20. public void Setage (int.) {
  21. this.age = age;
  22. }
  23. @Override
  24. public boolean equals (Object obj) {
  25. if (this= = obj) return true;
  26. if (obj = = null) return false;
  27. if (getclass () = Obj.getclass ()) return false;
  28. People other = (people) obj;
  29. if (age! = other.age) return false;
  30. if (firstName = = null) {
  31. if (other.firstname! = null) return false;
  32. } Else if (!firstname.equals (other.firstname)) return false;
  33. if (lastName = = null) {
  34. if (other.lastname! = null) return false;
  35. } Else if (!lastname.equals (other.lastname)) return false;
  36. return true;
  37. }
  38. }

In this example, we stipulate that a person is the same person if the surname, first name, and age are the same. Of course you can also add other properties, such as the need to have the same ID number, in order to determine the same person, you can add to the Equals method to determine the ID number!

The 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.