当前位置:网站首页>The difference between equals and = =

The difference between equals and = =

2022-07-26 10:21:00 Haha

Say first conclusion :
stay Java in equals And == Both are used to judge whether two data are equal , And they all have their own application scenarios .
Let's start with logical operators == , For basic data types , It compares values , For reference types , It compares the memory address of the object , Judgment Two quotes Whether to point to the same object in heap memory . because Java Language only has value passing , So for == Come on , The comparison is all worth ( Only the value of the basic data type is the value itself , The value of the reference type is the object address );
equals yes Object Class member methods , Every class can override this method , If there is no override in the custom class equals Method , It will inherit Object class equals Method , Its function is similar to that of logical operators == identical .
Practical application , Custom classes often override Object class equals Method , It is generally used to compare whether the contents of two independent objects are the same ( It depends on the implementation of specific methods ).

We analyze it concretely through a simple memory model :

In the figure a And b Is the basic data type (int a = 5 ,int b = 5);
str1 And str2 by String type (String class Object Class equals Member method );
user1 And user2 For custom class objects ( There is no override in this class equals Method , Used with String The value of type is used as a reference , Verify the above conclusion )

 Insert picture description here

Code implementation :

/** *  Test class  */
public class Demo {
    
    public static void main(String[] args) {
    
        int a = 5;
        int b = 5;
        String str1 = new String("abc");
        String str2 = new String("abc");
        User user1 = new User(" Zhang San ");
        User user2 = new User(" Zhang San ");

        // Basic data type does not equals Method 
        System.out.println(a == b);// Expected operating results true

        //String Class overridden equals Method , Used to compare whether the contents of two independent objects are the same 
        System.out.println(str1 == str2);// Expected operating results false
        System.out.println(str1.equals(str2));// Expected operating results true

        // Custom class objects are not overridden equals( Logical operators  == And equals The method works the same , The expected operation results are consistent )
        System.out.println(user1 == user2);// Expected operating results false
        System.out.println(user1.equals(user2));// Expected operating results false


    }
}

/** *  Customize User class  */
class User {
    
    private String name;

    public User(String name) {
    
        this.name = name;
    }

    public String getName() {
    
        return name;
    }

    public void setName(String name) {
    
        this.name = name;
    }
}

Running results :

D:\installPath\Java\jdk1.8.0_121\bin\java.exe "-javaagent:D:\installPath\IntelliJ IDEA 2019.1.4\lib\idea_rt.jar
true
false
true
false
false

Process finished with exit code 0

The running result is in line with the expectation

expand
Customize User Class is also overridden equals Method , Let's see whether the running results meet the expectations ( Develop a good programming habit , rewrite equals Method, also remember to rewrite hashCode Method )

Code example :

import java.util.Objects;

/** *  Test class  */
public class Demo {
    
    public static void main(String[] args) {
    
        int a = 5;
        int b = 5;
        String str1 = new String("abc");
        String str2 = new String("abc");
        User user1 = new User(" Zhang San ");
        User user2 = new User(" Zhang San ");

        // Basic data type does not equals Method 
        System.out.println(a == b);// Expected operating results true

        //String Class overridden equals Method , Used to compare whether the contents of two independent objects are the same 
        System.out.println(str1 == str2);// Expected operating results false
        System.out.println(str1.equals(str2));// Expected operating results true

        // Custom class objects have also been overridden equals Method 
        System.out.println(user1 == user2);// Expected operating results false
        System.out.println(user1.equals(user2));// Expected operating results true


    }
}

/** *  Customize User Class overridden equals And hashCode Method  */
class User {
    
    private String name;

    public User(String name) {
    
        this.name = name;
    }

    public String getName() {
    
        return name;
    }

    public void setName(String name) {
    
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
    
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return Objects.equals(name, user.name);
    }

    @Override
    public int hashCode() {
    
        return Objects.hash(name);
    }
}

Running results :

D:\installPath\Java\jdk1.8.0_121\bin\java.exe "-javaagent:D:\installPath\IntelliJ IDEA 2019.1.4\lib\idea_rt.jar
true
false
true
false
true

Process finished with exit code 0

The running result is in line with the expectation

原网站

版权声明
本文为[Haha]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/207/202207261016234963.html