The difference and understanding between = = and equals in Java

Source: Internet
Author: User

Difference:

"= =" Compares two references in memory to the same object (that is, the same memory space), that is, the storage location in the memory space is consistent.

If two objects have the same reference (when pointing to the same object), the "= =" operator returns true, otherwise flase is returned.

Note: If you are interested in memory allocation and variable storage location (heap, stack, method area constant pool, method area static area) You can go to see this blog, which is written in very detailed. It's a great help for the people I'm learning from, and I understand a lot of things, and it still needs to be digested.

The Equals method is provided by object objects that can be overridden by subclasses

The default implementation of the object class is as follows:

1  Public Boolean equals (Object obj) {23     return ( this = = obj); 4 5 }

The default implementation returns true only if the object is compared to itself, which is equivalent to "= =".

Many of the classes in Java (The String class date class file Class) have overridden the Equals method, which takes examples of common string classes.

1  Public classTest {2      Public Static voidMain (string[] args) {3String str1 = "abc";4String str2 = "abc";5System.out.println (STR1==STR2);//true6 7 8String STR3 =NewString ("ABC");9String STR4 =NewString ("ABC");TenSystem.out.println (STR3==STR4);//false OneSystem.out.println (Str3.equals (STR4));//true A     } -}

In the 5th row of the example above, we see that the result of str1 = = Str2 is True

Analysis Reason:

First look at the 3rd line of code, first create a String class object reference variable str1 in the stack, and then by reference to the string constant pool to find there is no "ABC", if not, put "ABC" into the string constant pool. There is no "ABC" in the Constant pool. That is , "ABC", which has been created at compile time (defined directly in double quotes), is stored in a constant pool. (If you don't understand the constant pool, look at the blog I recommended above)

The 4th line of code also creates an object reference to the String class str2, then by reference to the string constant pool to find there is no "ABC", if not, then "ABC" into a string constant pool, and make str2 point "abc", if there is already "ABC" directly to the str2 point "abc". Here we have stored the string "ABC" in the third line of code in a constant pool. So the str2 and str1 point to the same "ABC", which returns true.

Lines 8th and 9th create 2 objects respectively, STR3 and STR4 point to different objects, that is, the storage location in the memory space described above is different. therefore STR3 = = StR4 return is certainly false.

The 11th line of code str3.equals (STR4) returns True

Because the string class overrides the Equals method, it compares whether the data stored in the memory space is the same. The string "abc" is stored here and returns TRUE.

summary:

java language in the Equals method is actually to the developer to overwrite, let the developer to define Two objects satisfying what conditions are equal.

So we can not simply say that equals exactly what is compared. You want to know what the Equals method of a class means to see the definition. (The above is from the user's answer)

What we want to understand is the purpose of the equals existence.

If you want to see a more detailed introduction can go to see this blog http://www.cnblogs.com/bluestorm/archive/2012/03/02/2377615.html

My first blog to this end, as a Java beginner, in order to summarize the knowledge of the above written content is also very superficial, if there are shortcomings and errors welcome correct.

Differences and understandings between = = and equals 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.