Distinguish between equal method and equals method in Java
The difference between equal and equals in Java requires specific code examples
In Java, we often need to compare whether two objects are equal. In this case we usually use equal method or equals method in Java. Although they look similar, they have some important differences. This article will introduce the difference between equal and equals and explain it through specific code examples.
First, let’s take a look at the equal method. The equal method is a method in the Object class, and all objects in Java can call this method. This method uses reference comparison, that is, to determine whether two objects have the same reference. Two objects are equal if their references are the same. The following is a sample code:
String str1 = new String("Hello"); String str2 = new String("Hello"); boolean result = str1.equals(str2); System.out.println(result); // 输出结果为 true
In the above code, we created two string objects str1 and str2, whose values are "Hello". However, these two objects are created separately through the new keyword, so their references are different. However, since the String class overrides the equals method, when we call str1.equals(str2), it will return true, indicating that the two strings are equal.
Next, let’s take a look at the equals method. The equals method is a public method of the Object class. We can override it to compare the contents of two objects. By default, the equals method still uses reference comparison. If we want to customize the comparison rules, we must override the equals method. The following is a sample code:
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; if (age != person.age) { return false; } return name != null ? name.equals(person.name) : person.name == null; } // 省略hashCode方法和其他方法 }
In the above sample code, we created a Person class and overridden the equals method. When overriding the equals method, we first determine whether the references of the two objects are the same. If they are the same, we can directly return true. Then we determine whether obj is empty and belongs to the same class, then convert obj to Person type, and compare whether the values of name and age are equal.
The following is a sample code for using Person:
Person person1 = new Person("Tom", 20); Person person2 = new Person("Tom", 20); boolean result = person1.equals(person2); System.out.println(result); // 输出结果为 true
In the above code, we created two Person objects, person1 and person2, with the same name and age values. When we call person1.equals(person2), because we have rewritten the equals method, the values of name and age will be compared during comparison, so the returned result is true.
To sum up, equal and equals are methods commonly used in Java to compare whether objects are equal. The equal method is a method of the Object class, which determines whether two objects are equal through reference comparison; the equals method is a customized comparison method that allows you to customize comparison rules. In actual development, we should choose to use these two methods to compare objects according to specific needs.
The above is the detailed content of Distinguish between equal method and equals method in Java. 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

AI Hentai Generator
Generate AI Hentai for free.

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



How does Java use the join() function of the String class to concatenate multiple strings into one string? In Java, the String class is a commonly used class used to represent strings. It provides many methods for manipulating strings, one of the important methods is the join() function. This function can concatenate multiple strings into one string, and you can specify a delimiter to separate each string. This article will introduce how to use the join() function to implement string splicing operations. UseStri

Interpretation of Java documentation: Detailed explanation of the length() method of the String class. The String class is one of the most commonly used classes in the Java language. It provides a series of methods for operating strings. Among them, the length() method is one of the commonly used methods in the String class. This article will provide a detailed explanation of the length() method of the String class and provide specific code examples. 1. The length() method is defined in the Java documentation, length of the String class

How does Java use the concat() function of the String class to concatenate two strings? In Java, the String class is a very commonly used class that provides many methods for manipulating strings. One of the most commonly used methods is the concat() function, which can be used to concatenate two strings. The prototype of the concat() function is as follows: publicStringconcat(Stringstr) This function accepts a parameter str and connects it to the calling method.

How does Java use the getBytes() function of the String class to convert a string into a byte array? In Java, the String class stores strings in character form, and sometimes we need to convert strings into byte arrays for processing. This You can use the getBytes() function of the String class to complete the conversion. The getByte() function will encode the string into the specified byte array and return the byte array. Below I will explain how

char in Java represents a primitive data type that stores a single Unicode character, using two bytes, ranging from 0x0000 to 0xFFFF, and the default value is '\u0000'. It is used to store individual characters or as part of a string.

How to convert a string to uppercase in Java using toUpperCase() function of String class In Java, String class is a very commonly used class that provides many methods for processing strings. One very useful method is toUpperCase(), which converts a string to uppercase. The use of toUpperCase() method is very simple, just call this method. Here is a sample code that shows how to use toUp

How to use the indexOf() function of the String class in Java to find specified characters or substrings in a string Introduction: In Java, the String class is one of the most commonly used classes, and it provides many methods to operate strings. The indexOf() function is one of the methods used to find specified characters or substrings in a string. This article will introduce in detail how to use the indexOf() function of the String class in Java to implement string search operations, and provide some sample code to help readers better

How to convert a string into a character array in Java using toCharArray() function of String class In Java, String class is a class that represents strings and provides many useful methods to handle strings. Among them, the toCharArray() function is a very practical method in the String class, which can convert a string into a character array. This article details how to use the toCharArray() function to convert a string into a character array and provides code examples.
