Home  >  Article  >  Java  >  How to correctly use the equals() method of the Java Object class?

How to correctly use the equals() method of the Java Object class?

WBOY
WBOYforward
2023-05-07 08:13:07804browse

Basic concept:

  • The Object class is located in the java.lang package. The java.lang package contains the most basic and core classes of Java. During compilation Will be imported automatically;

  • The Object class is the ancestor of all Java classes. Every class uses Object as its superclass. All objects (including arrays) implement the methods of this class. You can use a variable of type Object to point to any type of object

equals() method:Compare two objects to see if they are the same

If two objects have the same type and the same attribute values, the two objects are said to be equal. If two reference objects refer to the same object, the two variables are said to be the same. The prototype of the equals function defined in the Object class is:

public boolean equals(Object);It is used to determine whether two objects are the same, not whether they are equal

How to correctly use the equals() method of the Java Object class?

  • ①Can only handle reference type variables

  • ②In the Object class, find equals() Are the address values ​​of the two reference variables still being compared equal?

package com.example.demo.test;

public class Person {

    private String userName;
    private String age;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}
package com.example.demo.test;

public class Test {

    public static void main(String[] args) {
        Person p1 = new Person();
        Person p2 = new Person();
        System.out.println(p1.equals(p2));
        System.out.println(p1 == p2);

        /*
         * String类重写了Object类的equals()方法,比较是两个对象的实体内容"是否完全相同。
         */
        String s1 = new String("AA");
        String s2 = new String("AA");
        System.out.println(s1.equals(s2));
        System.out.println(s1 == s2);
    }
}

How to correctly use the equals() method of the Java Object class?

What you can see from the running results is the equals method in Object What is compared is whether the two objects are the same,

and the equals method in the String class compares whether the values ​​of the strings are equal. Please look at the equals method in String.java

public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = value.length;
            if (n == anotherString.value.length) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = 0;
                while (n-- != 0) {
                    if (v1[i] != v2[i])
                        return false;
                    i++;
                }
                return true;
            }
        }
        return false;
    }

The above is the detailed content of How to correctly use the equals() method of the Java Object class?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete