Detailed explanation of the equals(Object) method in Java and its usage scenarios
In Java, the equals(Object) method is a very important method, which is used to Compares two objects for equality. In this article, we will explain in detail the usage scenarios and principles of the equals(Object) method, and introduce some common usage techniques.
1. The principle of equals(Object) method
In Java, all classes inherit from the Object class, and the equals(Object) method in the Object class is a default implementation. The equals(Object) method in the Object class actually compares whether the references of two objects are equal, that is, whether the two objects are the same object. The specific implementation is as follows:
public boolean equals(Object obj) {
return (this == obj);
}
In many cases, what we need to compare is whether the contents of two objects are equal. , not just quotes. Therefore, we need to override the equals(Object) method in a custom class to meet our needs.
2. Usage scenarios of equals(Object) method
When we need to compare the values of two basic types , you can use the == operator directly. For example, to compare two integers for equality:
int a = 10;
int b = 10;
if (a == b) {
System.out.println("a和b相等");
}
When we need to compare the values of two reference types, we should use the equals(Object) method. For example, compare two strings for equality:
String str1 = "Hello";
String str2 = "Hello";
if (str1.equals(str2)) {
System.out.println("str1和str2相等");
}
When we need to compare objects of custom classes, we should override the equals(Object) method in the class, to compare the contents of objects. For example, compare two Person objects for equality:
public class Person {
private String name; private int age; // 构造方法和其他方法省略 @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null || getClass() != obj.getClass()) { return false; } Person person = (Person) obj; return age == person.age && Objects.equals(name, person.name); }
}
Person person1 = new Person("Zhang San", 20);
Person person2 = new Person("Zhang San", 20);
if (person1.equals(person2)) {
System.out.println("person1和person2相等");
}
3. equals(Object) method Usage tips
When overriding the equals(Object) method, you need to follow the conventions of the equals(Object) method, that is, satisfy The following conditions:
When comparing the contents of objects, we can use the Objects.equals method provided by Java, which will automatically handle null values. Case. For example, to compare two strings for equality, use the following code:
String str1 = "Hello";
String str2 = "Hello";
if (Objects.equals(str1, str2) ) {
System.out.println("str1和str2相等");
}
You can avoid NullPointerException by using the Objects.equals method.
4. Summary
This article introduces the equals(Object) method in Java and its usage scenarios in detail. In actual development, we often need to compare whether the contents of two objects are equal, so it is very important to override the equals(Object) method. By following the convention of the equals(Object) method and using the Objects.equals method, we can compare the contents of objects more accurately and safely. I hope this article will help you gain a deeper understanding of the equals(Object) method and its usage scenarios.
The above is the detailed content of Detailed explanation of the usage and applicable scenarios of equals method in Java. For more information, please follow other related articles on the PHP Chinese website!