


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; }
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 } }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Start Spring using IntelliJIDEAUltimate version...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...
