The difference between "= =" and equal in Java

Source: Internet
Author: User

8 months after the official job search, I think it is time to take the time to thoroughly study the previous comparison of knowledge. This is the first step to self-growth!

For string "equal method" and "= =" has been a little mixed, today re-look at their two points of difference, recorded so that they do not forget in the future!

First say "= =":

the "= =" is used to compare whether the two string objects are stored in memory in the same address. For example

String test1 = "Test"; String test2 = "Test"; String test3 = new string (test2); String test4  =new string (test2); Blooean result1 = (test1==test2); Blooean result2 = (TEST3==TEST4);

Where: Result1 is true,result2 to false.

Cause: The program has a string caching mechanism at run time, when string test1 = "Test", the program first looks for the same string object from the cache pool, and if it does not regenerate, instead uses the string object in the cache pool If the same string object is not found in the string cache pool, a new string object is opened in memory. For Test1, when Test1 is established, the "test" string is placed in the cache pool, so when you run string test2 = "Test" the same object is fetched directly from the cache pool, and the memory address of Test1 and Test2 is the same, so RESULT1 is true. For new, each new one will open a memory area in memory, and the memory address of TEST3 and test4 is different, so result2 is false.

Say "Equal method":

The equal method is a method of the object class, and the equal method in the object class is also implemented using "= =", that is, if the equal method of the object class is inherited directly, it is also a comparison of the two objects in memory for the same address. However, the equal method that inherits from object in string is overwritten! The source code for the equal method in string is as follows:

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 =value.length; at             if(n = =anotherString.value.length) { -                 CharV1[] =value; -                 CharV2[] =Anotherstring.value; -                 inti = 0; -                  while(n--! = 0) { -                     if(V1[i]! =V2[i]) in                             return false; -i++; to                 } +                 return true; -             } the         } *         return false; $}

As you can see, the equal method in string is to compare the contents of two string objects.

The difference between "= =" and equal in Java

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.