Equal (Object obj) method for depth parsing of java.lang.String classes

Source: Internet
Author: User

Background: Recently in the study of static scanning things, encountered a rule: "Equals (Object obj)" should be overridden along with the "CompareTo (T obj)" method

And then I want to have a deep steak. What's the difference between equals and CompareTo?

First, let's look at how the Equals method under the Java.lang.String class is implemented.

 Public Booleanequals (Object anobject) {if( This==anobject) {//Determine the object's address is consistentreturn true; }        if(AnObjectinstanceofString) {//Determine if AnObject is a string type string anotherstring=(String) AnObject; Type Conversionsintn =value.length; if(n = =anotherString.value.length) {//Determine if the length of the character array is consistentCharV1[] =value; CharV2[] =Anotherstring.value; inti = 0;  while(n--! = 0{//From left to right to compare characters for equalityif(V1[i]! =V2[i])return false; I++; }                return true; }        }        return false; }

1. The address of the object is consistent returns True

2. Object address inconsistency but the type is consistent, the character array is the same length and each character is equal, returns True

3. Returns False if the argument is not of type string

4. Returns False if the argument is of type string but the length of the character array is inconsistent

5. If the argument is of type string, the character array is the same length, but left to right returns false if a character inconsistency is encountered

Equal (Object obj) method for depth parsing of java.lang.String classes

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.