The difference between equals() and == and the relationship with hashCode()

== and equals()

  • ==
    Its function is to judge whether the addresses of two objects are equal, that is, to judge whether the two objects are the same object. Basic data type == compares values, and reference data type == compares memory addresses.


  • The role of equals is to determine whether two objects are equal, and it cannot be used to compare variables of basic data types. There are two main uses:

    1. The class does not cover the equals() method. When comparing two objects of this class through equals(), it is equivalent to comparing the two objects through ==. The default used is the equals() method of the Object class.
    2. The class covers the equals() method. Generally, we cover the equals() method to make the contents of two objects equal; if their contents are equal, it returns true (that is, the two objects are considered equal).
       

for example

public class test1 {
    
    
    public static void main(String[] args) {
    
    
    	String a1 = "a1"; // 放在常量池中
        String b1 = "b1"; // 从常量池中查找
        String a2 = new String("a2"); // a 为一个引用
        String b2 = new String("b2"); // b为另一个引用,对象的内容一样
        
        if (a1 == b1) // true,同一对象
            System.out.println("a1==b1");
        if (a2 == b2) // false,非同一对象
            System.out.println("a2==b2");
        if (a2.equals(b2)) // true,比较内容是否相等
            System.out.println("a is equals to b");
        }
    }
}

Explanation : The equals() method in String has been rewritten, because the equals() method in Object compares the memory address of the object, while the equals() method in String compares the value of the object.

 

hashCode()

The function of hashCode() is to obtain a hash code, also known as a hash code, and return an int integer. The function of the hash code is to determine the index position of the object in the hash table. hashCode() is defined in Object.java, so any class in Java contains the hashCode() function.

A hash table (hash table) refers to a data structure that is directly accessed based on the key value. It accesses the record by mapping the key code value to a position in the table to speed up the search.

Why is there a hashCode()?
Take how HashSet checks for duplication as an example to illustrate why there is a hashCode: When you add an object to a HashSet, HashSet will first calculate the hashcode value of the object to determine the position where the object is added, and it will also be different from that position. The hashcode value of the added object is compared. If there is no matching hashcode, HashSet will assume that the object does not appear repeatedly. But if an object with the same hashcode value is found, the equals() method will be called to check whether the objects with equal hashcode are really the same. If the two are the same, HashSet will not let it join the operation successfully. If they are different, they will be hashed to another location. In this way, we greatly reduce the number of times using equals() and increase the execution speed.
 
HashCode() and equals() related regulations

  1. If the two objects are equal, the hashcode must also be the same

  2. The two objects are equal, and the equals() method on the two objects respectively returns true

  3. Two objects have the same hashcode value, they are not necessarily equal

  4. Therefore, if the equals() method is overridden, the hashCode() method must also be overridden. Otherwise, although rewriting equals() to compare the two objects and get the content of the two objects to be equal, their hashCode is still not equal;

  5. The default behavior of hashCode() is to generate unique values ​​for objects on the heap. If you do not override hashCode(), even if two objects point to the same data, they will not be equal

Guess you like

Origin blog.csdn.net/weixin_43901865/article/details/112565799