Java equals () & = =

Source: Internet
Author: User

The similarities and differences between equals () & = = In Java can be obtained by flipping through the JDK source Java.lang.String.equals (Object anobject) to obtain a perceptual knowledge:

1 /**2 * Compares this string to the specified object. The result is {@code3 * true} if and only if the argument are not {@codenull} and is a {@code4 * String} object that represents the same sequence of characters as this5 * object.6      *7      * @paramAnObject8 * The object to compare this {@codeString} against9      *Ten      * @return  {@codetrue} If the given object represents a {@codeString} One * Equivalent to this string, {@codefalse} otherwise A      * -      * @see#compareTo (String) -      * @see#equalsIgnoreCase (String) the      */ -      Public Booleanequals (Object anobject) { -      if( This==anobject) { -          return true ; +      } -      if(AnObjectinstanceofString) { +String anotherstring =(String) anobject; A          intn =count; at          if(n = =anotherstring.count) { -            CharV1[] =value; -            CharV2[] =Anotherstring.value; -            inti =offset; -            intj =Anotherstring.offset; -             while(n--! = 0) { in               if(v1[i++]! = v2[j++]) -                return false ; to           } +            return true ; -          } the      } *      return false; $}

From the above you can see the case where the java.lang.String.equals (Object AnObject) method returns true:

    1. Compare yourself with yourself;
    2. Two objects are string and the contents are the same.

To be aware of:

    1. Java does not support operator overloading (Operator overloading);
    2. In Java, Equals () and hashcode () are closely related, and the two methods are rewritten (overriding), so the specific meaning of equals () depends on the code implementation.

Java equals () & = =

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.