Home > Java > javaTutorial > How to Properly Override the equals() Method in Java for Custom Object Comparisons?

How to Properly Override the equals() Method in Java for Custom Object Comparisons?

Barbara Streisand
Release: 2024-12-27 08:16:09
Original
373 people have browsed it

How to Properly Override the equals() Method in Java for Custom Object Comparisons?

Overriding the Equals Method in Java

In Java, the equals method is responsible for determining whether two objects are equal. By default, the equals method compares object references, which is not the desired behavior for many custom classes. To override the equals method and define custom equality semantics, the following guidelines should be followed:

  • Check for null: The method should first check if the passed object is null and return false if it is.
  • Check for type: Verify that the passed object is of the same type as the calling object. If not, return false.
  • Compare fields: Determine the relevant fields that define equality and compare their values.
  • Use operator precedence: Use operator precedence to ensure that potential null values are checked before they are accessed.

Solution:

The provided code attempted to compare an Integer field (age) using the equals method, which is intended for Strings only. To correct this, the equality check for the age field should be replaced with a comparison using the == operator:

...
} else {
    People otherPeople = (People) other;
    result = name.equals(other.name) && other.age == age;
}
...
Copy after login

Usage:

The following code demonstrates how to use the overridden equals method:

...
// Add several Person objects to an ArrayList
people.add(new Person("Subash Adhikari", 28));
people.add(new Person("K", 28));
people.add(new Person("StackOverflow", 4));
people.add(new Person("Subash Adhikari", 28));

// Compare each pair of Person objects using the overridden equals method
for (int i = 0; i < people.size() - 1; i++) {
    for (int y = i + 1; y <= people.size() - 1; y++) {
        boolean check = people.get(i).equals(people.get(y));
        System.out.println("-- " + people.get(i).getName() + " - VS - " + people.get(y).getName());
        System.out.println(check);
    }
}
...
Copy after login

This code will produce the following output:

-- Subash Adhikari - VS - K false
-- Subash Adhikari - VS - StackOverflow false
-- Subash Adhikari - VS - Subash Adhikari true
-- K - VS - StackOverflow false
-- K - VS - Subash Adhikari false
-- StackOverflow - VS - Subash Adhikari false
Copy after login

This output demonstrates that the overridden equals method is correctly comparing the Person objects based on their name and age fields.

The above is the detailed content of How to Properly Override the equals() Method in Java for Custom Object Comparisons?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template