Interviewer: talk about the difference between equals() and = =

introduction

Talk about the difference between equals() and = =. This is a very classic question. It is not particularly difficult. It is also asked in interviews; However, many young partners have a blank mind during the interview and lose the offer; So here I will introduce the difference between the two in detail, hoping to be helpful to you;

Packaging

Packaging classes are nothing more than byte, short, Integer, long, float, double, character and Boolean, so here I use Integer as an example;
For the wrapper class, remember that the equals() method of the wrapper class has been rewritten by Sun, so the eqauls() method of the wrapper class compares the content, and = = compares the addresses of the two objects;
Test code:

package code01;

public class IntegerTest01 {
    public static void main(String[] args) {
        Integer num1 = 1000;
        Integer num2 = 1000;

        System.out.println("use'=='compare num1 and num2: ");
        System.out.println(num1==num2); // false
        System.out.println("use'equals'compare num1 and num2: ");
        System.out.println(num1.equals(num2)); // true
    }
}

Output results:

use'=='compare num1 and num2: 
false
 use'equals'compare num1 and num2: 
true

Because num1 and num2 store different object addresses, the comparison using = = is false, while the data stored by num1 and num2 objects are 1000, and the comparison will return true through the equals method;

Integer exception

Of course, sometimes you will find a magical thing when using Integer. Let me show you a code first:

package code01;

public class IntegerTest02 {
    public static void main(String[] args) {
        Integer num1 = 100;
        Integer num2 = 100;

        System.out.println("use'=='compare num1 and num2: ");
        System.out.println(num1==num2); // true
        System.out.println("use'equals'compare num1 and num2: ");
        System.out.println(num1.equals(num2)); // true
    }
}
use'=='compare num1 and num2: 
true
 use'equals'compare num1 and num2: 
true

It can be found that = = turns out to be true. Aren't num1 and num2 the addresses of the two stored objects? In fact, this is the specific design of Java for Integer. It automatically boxes integers between - 128 and 127 into Integer instances. Therefore, when calling integers in this range, these encapsulated numbers will be called directly, and there is no need to rebuild the Integer instance;
In fact, this kind of data caching has many advantages. Many places will use this method to improve the performance of the system. If you are interested, you can learn about it;

String class

The String class is very similar to the wrapper class. Sun also rewrites the equals method of String and compares the object content;
However, for the String class, it should be noted that after the String object is created, it will be stored in the constant pool. When it is called again, it will directly reference the String object in the constant pool; However, if you create a new String object yourself, the object will not be stored in the constant pool;
Test code:

package code01;

public class StringTest {
    public static void main(String[] args) {
        String str1 = "It's a lovely day";
        String str2 = "It's a lovely day";
        String str3 = new String("It's a lovely day");
        String str4 = new String("It's a lovely day");

        System.out.println("==========str1 and str2===========");
        System.out.println("use'=='compare str1 and str2: ");
        System.out.println(str1==str2); // true
        System.out.println("use'equals'compare str1 and str2: ");
        System.out.println(str1.equals(str2)); // true

        System.out.println("==========str1 and str3===========");
        System.out.println("use'=='compare str1 and str3: ");
        System.out.println(str1==str3); // false
        System.out.println("use'equals'compare str1 and str3: ");
        System.out.println(str1.equals(str3)); // true

        System.out.println("==========str3 and str4===========");
        System.out.println("use'=='compare str3 and str4: ");
        System.out.println(str4==str3); // false
        System.out.println("use'equals'compare str3 and str4: ");
        System.out.println(str4.equals(str3)); // true
    }
}

Output results:

use'=='compare str1 and str2: 
true
 use'equals'compare str1 and str2: 
true
==========str1 and str3===========
use'=='compare str1 and str3: 
false
 use'equals'compare str1 and str3: 
true
==========str3 and str4===========
use'=='compare str3 and str4: 
false
 use'equals'compare str3 and str4: 
true

Let me explain the code:
First, the String object created by str1 is stored in the constant pool. Then, when str2 uses the same String again, it will directly reference the address of the corresponding String in the constant pool. Therefore, the reference addresses of str1 and str2 are the same, and the object contents are the same;

Str3 is a newly created object, so the string created by str3 will not be stored in the constant pool, which means that the objects referenced by str1 and str3 are different, so the addresses are different, but the contents of the objects are the same;

The same principle applies to str3 and str4: the object addresses are different, and the object contents are the same;

You can try it yourself and you'll see;

Custom type

For user-defined types or data types other than wrapper classes and String classes, equals compares the object address rather than the content. Here I will give an example of user-defined data types;
Example code:

package code01;

public class MyTest01 {
    public static void main(String[] args) {
        Person01 person1 = new Person01("Zhang San", 18);
        Person01 person2 = new Person01("Zhang San", 18);

        System.out.println("use'=='compare person1 and person2: ");
        System.out.println(person1==person2); // false
        System.out.println("use'equals'compare person1 and person2: ");
        System.out.println(person1.equals(person2)); // false
    }
}
// Custom type
class Person01 {
    String name;
    int age;

    // Parameterless constructor
    public Person01() {
    }

    // Parametric constructor
    public Person01(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

Output results:

use'=='compare person1 and person2: 
false
 use'equals'compare person1 and person2: 
false

You can see that both are false, because here equals and = = are the addresses of the comparison objects, so if we want to compare the contents of the objects, we need to rewrite the equals method according to the user-defined comparison rules;
Rewritten version:

package code01;

import java.util.Objects;

public class MyTest02 {
    public static void main(String[] args) {
        Person02 person1 = new Person02("Zhang San", 18);
        Person02 person2 = new Person02("Zhang San", 18);

        System.out.println("use'=='compare person1 and person2: ");
        System.out.println(person1==person2); // false
        System.out.println("use'equals'compare person1 and person2: ");
        System.out.println(person1.equals(person2)); // true
    }
}
// Custom type
class Person02 {
    String name;
    int age;

    // Parameterless constructor
    public Person02() {
    }

    // Parametric constructor
    public Person02(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Override the equals method
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Person02)) return false;
        Person02 person02 = (Person02) o;
        // Only when age and name are the same
        return age == person02.age && Objects.equals(name, person02.name);
    }
}

Output results:

use'=='compare person1 and person2: 
false
 use'equals'compare person1 and person2: 
true

You can see that the rewritten equals can be compared according to user-defined rules;

summary

In fact, I have said so much. To sum up:
The equals() method of String class and wrapper class has been overridden to directly compare the object content;

The equals() method of other data types compares the object address. If you want to compare the content, you need to rewrite the method;

==Are the addresses of the comparison objects;

Welcome your comments!

Keywords: Java JavaSE equals

Added by seavers on Sun, 19 Dec 2021 07:19:23 +0200