Java Interview question: Equals () method and = = Difference

Source: Internet
Author: User

http://bbs.csdn.net/topics/390000725

Summarize:

equals before the rewrite and = =, after rewriting, equals as long as the content is true

equals = = is generally equivalent, but for string types, it overrides the Equals method and compares the contents. By default, two is the reference address of the comparison, unless you override the Equals method.

equals Source:

  Public Booleanequals (Object anobject) {if( This==anobject) {        return true; }    if(AnObjectinstanceofString) {String anotherstring=(String) AnObject; intn =count; if(n = =anotherstring.count) {CharV1[] =value; CharV2[] =Anotherstring.value; inti =offset; intj =Anotherstring.offset;  while(n--! = 0) {            if(v1[i++]! = v2[j++])            return false; }        return true; }    }    return false; }

Moderator Answer:

"but often say = = on both sides of the object is compared by address, and equals () is based on the content in the comparison" this sentence in the exclusion of the root class object and did not rewrite the Equals method of the case is correct.

That is, the implementation of equals in the object class is used = =
Other equals methods that inherit object basically override this method, such as the Equals method of the String class:

xml/html Code?
123456789101112 equalspublic boolean equals(Object anObject)Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object. Overrides:equals in class ObjectParameters:anObject - The object to compare this String against Returns:true if the given object represents a String equivalent to this string, false otherwiseSee Also:compareTo(String), equalsIgnoreCase(String)



This is the Equals method of string source code:

Java Code?
12345678910111213141516171819202122 public booleanequals(Object obj)    {        if(this== obj)            returntrue;        if(obj instanceofString)        {            String s = (String)obj;            inti = count;            if(i == s.count)            {                charac[] = value;                charac1[] = s.value;                int j = offset;                intk = s.offset;                while(i-- != 0)                     if(ac[j++] != ac1[k++])                        returnfalse;                returntrue;            }        }        returnfalse;    }

Java Interview question: Equals () method and = = Difference

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.