Java record-23-equals method and equals sign Parsing

Source: Internet
Author: User

Java record-23-equals method and equals sign Parsing
Equals method and equals sign parsing 1. in the source code of Java, java. lang. the Object class has an equals method. This method is very simple, but it is often used and often appears during interviews. 2. The following is the implementation of the Object equals method in java source code. Its implementation is very simple:

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

 

3. Since the equals () method is defined in the Object class, every class in Java has this method. 4. For the equals method of the Object class, it is used to determine whether the reference that calls the equals method is consistent with the passed reference, that is, whether the two references point to the same Object. 5. object obj1 = new Object (); Object obj2 = new Object (); System. out. println (obj1.equals (obj1); // true System. out. println (obj1.equals (obj2); // The two outputs above verify the above statement. If the same object is compared using the equals method, true is returned, if two objects use equals for comparison, false is returned. 6. string s1 = new String ("aa"); String s2 = new String ("aa"); System. out. println (s1.equals (s2); when we compare two different String objects using equals, we will be confused and return true at this moment. This refuted the above statement. Why? 7. in the face of the String equals method, we need to think carefully about some issues: All classes in Java inherit from the Object, and the String is no exception, however, its equals method is obviously not executed according to the Object class method. Does String overwrite the equals method of the parent class Object class? 8. With the above questions, we can view the source code for writing strings and see how the equals method is implemented. In programming learning, the best way to solve the problem is to view the source code, the source code can tell you the truth.
public boolean equals(Object anObject) {    if (this == anObject) {        return true;    }    if (anObject instanceof String) {        String anotherString = (String)anObject;        int n = count;        if (n == anotherString.count) {        char v1[] = value;        char v2[] = anotherString.value;        int i = offset;        int j = anotherString.offset;        while (n-- != 0) {            if (v1[i++] != v2[j++])            return false;        }        return true;        }    }    return false;}

 

9. From the source code above, we can see that the String class overrides the equals method of the parent class Object. For the equals method of the String class, it is used to determine whether the String that calls this method is consistent with the content of the passed String. The length and content are the same. 10. to sum up, if the equals method is used, if the class used does not overwrite the equals method of the parent class Object, it will compare whether the reference of the two objects points to the same Object, that is, it is equivalent to the double equal sign; if the class used overrides the equals method of the parent class Object, such as the String class, what is compared by the equals method now depends on the overwritten content, string equals compares the content of the two strings. 11. from the above conclusion, we can conclude that the equals method must be used when determining the equality of String, instead of using =, the first step of the equals method is to judge =, only the equals method can be used to determine whether the content of the two strings is consistent. 12. In general, = is used to determine whether the two references are consistent, and the equals method is used to determine whether the content is consistent. This statement is not accurate. = Is to judge whether the two references are consistent. This is true, but when the equals method is used to determine whether the two reference addresses are consistent when the Object method is not overwritten, if the content is rewritten, the String is used to determine whether the content is consistent. When writing our own classes, we can rewrite the equals method to achieve content consistency.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.