The difference between = = and equals methods in Java string comparison

Posted by ensanity on Sat, 07 Dec 2019 18:32:36 +0100

Code demonstration:

public class Test {
    public static void main(String[] args){
        // Case 1
        String str1 = new String("hello");
        String str2 = "hello";
        System.out.println(str1 == str2);  // false
        System.out.println(str1.equals(str2));  // true
        
        // Case two
        String str3 = "world";
        String str4 = "world";
        System.out.println(str3 == str4);  // true
        System.out.println(str3.equals(str4));  // true
    }
}

 

 Problem Description:

  •  " == " Its application scenarios are roughly divided into two categories,first kindIs used to compare basic data types( Java Eight basic data types: byte,short,int,float,long,double,char,boolean),They compare values directly between the underlying data types.Second categoryIt is used to compare objects. It will directly compare two objectsMemory address,That is, unless two objects point to the same address, they are always false.
  • equals()The method is Object Methods in the base class, so any class will inherit this method, but some classes override this method, such as String Class, we can combine String class equals()Source code to explain.
// Source code of the overridden equals() method in the String class:
    public boolean equals(Object anObject) {
        if (this == anObject) {  // First, judge whether it is the same object. If it is true, return it directly
            return true;
        }
        if (anObject instanceof String) {  // Then judge whether the instance passed in is a String. If so, continue to judge
            String anotherString = (String)anObject;
            int n = value.length;
            if (n == anotherString.value.length) {  // First, judge the length of two strings. If not equal, return false directly
                char v1[] = value;  // At the bottom of the string is an array of characters called value to load the data.
                char v2[] = anotherString.value;
                int i = 0;
                while (n-- != 0) { 
                    if (v1[i] != v2[i])  // Compare each character for equality
                        return false;
                    i++;
                }
                return true;
            }
        }
        return false;
    }

            

Conclusion:

  • In case 1, str1 and str2 are different objects, but the contents are the same. Because "= =" is the memory address of the comparison object, false is returned. The overridden equals() method compares the value of the content, so it returns true
  • In case 2, why does str3 and str4 return true when compared with "=?"? Shouldn't false be returned? In fact, Java will maintain a public storage pool. If a String object is built directly from a pure String, it will first check whether there is such a String in the storage pool. If not, it will create one. If there is, it will point the variable to an existing object in the storage pool. So str3 and str4 point to the same object, so return true.

Topics: Java