Home> Java> javaTutorial> body text

Detailed explanation of the usage and applicable scenarios of equals method in Java

WBOY
Release: 2024-01-11 11:55:00
Original
821 people have browsed it

Detailed explanation of the usage and applicable scenarios of equals method in Java

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);
Copy after login

}

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

  1. Compare the values of basic types

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相等");
Copy after login

}

  1. Compare reference type values

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相等");
Copy after login
Copy after login

}

  1. Comparison of custom classes

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); }
Copy after login

}

Person person1 = new Person("Zhang San", 20);
Person person2 = new Person("Zhang San", 20);
if (person1.equals(person2)) {

System.out.println("person1和person2相等");
Copy after login

}

3. equals(Object) method Usage tips

  1. Follow the conventions of the equals(Object) method

When overriding the equals(Object) method, you need to follow the conventions of the equals(Object) method, that is, satisfy The following conditions:

  • Reflexivity: For any non-null reference value x, x.equals(x) should return true.
  • Symmetry: For any non-null reference values x and y, if x.equals(y) returns true, y.equals(x) should also return true.
  • Transitivity: For any non-null reference values x, y, and z, if x.equals(y) returns true, and y.equals(z) also returns true, then x.equals(z) It should also return true.
  • Consistency: For any non-null reference values x and y, if the value in the object does not change, the result of x.equals(y) should always remain consistent.
  • Non-nullability: For any non-null reference value x, x.equals(null) should return false.
  1. Use the Objects.equals method

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相等");
Copy after login
Copy after login

}

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!

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 Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!