Java--The difference between "equals()" and "==" (detailed full version)

 

table of Contents

One: the use of "=="

Two: the use of qeuals() method

Three: Summary of differences


At the forefront. First correct a misunderstanding of thinking: equals () method is more valuable.

The equals() method compares values ​​(this statement is wrong)

One: the use of "=="

1) Can be used in basic data type variables and reference data type variables

2) If the comparison is a basic data type variable: the comparison is whether the data saved by the two variables are equal. (It does not have to be the same type)

     If the comparison is a reference data type variable: compare whether the address values ​​of the two objects are the same, that is, whether the two references point to the same entity.

                                                           (Supplement: At this time, the variable types on both sides of "==" must be the same)

Comparison example of basic data type variables

        int i = 10;
        int j = 10;
        double d = 10.0;
        System.out.println(i == d); // true 因为存在类型提升,所以可以进行比较
        char ch = 10;
        System.out.println(ch == i); // true .
        // 注意上面返回的true,因为ch对应的值是10,而不是说'10',不然字符存储类1两位字符呀
        char c1 = 'A';
        char c2 = 65;
        System.out.println(c1== c2);   //true

Examples of reference data types

public class Customer {
    public String name;
    private int age;

    public Customer(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

public class equals {
    public static void main(String[] args) {

        Customer cu1 = new Customer("Tom",11);
        Customer cu2 = new Customer("Tom",11);
        System.out.println(cu1 == cu2); // false
}

Special attention: Examples of two different definitions and initialization methods of String

public class equals {
    public static void main(String[] args) {
        String s1 = "BB";
        String s2 = "BB";
        System.out.println(s1 == s2); // TRUE
        String s3 = new String("BB");
        String s4 = new String("BB");
        System.out.println(s3 == s4);  //FALSE
    }
}

Two: the use of qeuals() method

1) equals() is a method, not an operator

2) equals () method can only be applied to reference data types

3) The definition of equals() in the Object class:

public boolean equals(Object obj){
    return (this == obj);
}

[Note]: equals() in the Object class is still the same as "==", the comparison is still the address of the reference data type.

System.out.println(cu1.equals(cu2));  //flase

4) The above (3) is the equals() method in the most primitive Object, but the equals() method in the Object class is rewritten like String, Date, File, and wrapper classes.

Rewrite the sum, the comparison is not whether the addresses of the two references are the same, but whether the "entity content" of the two objects are equal, that is, whether the corresponding values ​​are equal.

        String str1 = new String("ye");
        String str2 = new String("ye");
        System.out.println(str1.equals(str2));// true

5) Normally, if our custom class uses equals(), it usually compares whether the "entity content" (value) of two objects is equal.

But our custom class corresponds to the equals() method in Object, so we need to rewrite equals() in the Object class.

The principle of rewriting: Compare whether the entity content of two objects are the same.

We can write by hand or automatically generate it through the editor. Below is the rewritten equals() method we manually wrote.

public class Customer {
    public String name;
    private int age;

    public Customer(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object obj) {
        if(this == obj){
            return true;
        }

        if(obj instanceof  Customer){
            Customer cust = (Customer) obj;
            //比较·两个对象的每个属性是否都相同
            if (this.age == cust.age && this.name.equals(cust.name)){
                // int类型的age使用==比较,String类型的name使用equals比较
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }
}

Three: Summary of differences

Guess you like

Origin blog.csdn.net/yezonghui/article/details/112516387