Home > Java > javaTutorial > How to Correctly Override the equals() Method in Java for Objects with String and Integer Fields?

How to Correctly Override the equals() Method in Java for Objects with String and Integer Fields?

DDD
Release: 2024-12-23 03:39:15
Original
800 people have browsed it

How to Correctly Override the equals() Method in Java for Objects with String and Integer Fields?

Overriding the Equals Method in Java

In Java, the equals method is a fundamental tool for comparing objects for equality. When you override the equals method in a custom class, you can define specific criteria for determining whether two objects of that class are equivalent.

Understanding the Problem

Consider a Person class with fields for name and age. To compare two Person objects for equality, you can override the equals method. However, if the age field is an integer, you might encounter an error when trying to compare it using the equals method, which is designed to compare strings.

Solution

To solve this issue, you can use the equality operator == to compare integers. Here's an example of an overridden equals method that handles both string and integer comparisons:

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }

    if (obj.getClass() != this.getClass()) {
        return false;
    }

    final Person other = (Person) obj;
    if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
        return false;
    }

    if (this.age != other.age) {
        return false;
    }

    return true;
}
Copy after login

In this modified code:

  • We first perform null checks and class checks to ensure that we're comparing two valid Person objects.
  • We compare the name fields using the equals method since they are strings.
  • We use the == operator to compare the age fields because they are integers.

Example Usage

Here's an example of how to use the overridden equals method:

public class Main {

    public static void main(String[] args) {
        List<Person> people = new ArrayList<>();
        people.add(new Person("John Doe", 30));
        people.add(new Person("Jane Doe", 25));

        // Check for equality using the overridden equals method
        boolean equal = people.get(0).equals(people.get(1));
        System.out.println(equal); // Output: false
    }
}
Copy after login

In this example, the equals method correctly determines that two Person objects with different names and ages are not equal.

The above is the detailed content of How to Correctly Override the equals() Method in Java for Objects with String and Integer Fields?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template