The difference between the "= =" of the Java Foundation and the Equals method

Source: Internet
Author: User

One, "= =" operator

The = = operator is specifically used to compare the values of two variables, that is, whether the value stored in the memory used to compare the variables is the same, to compare two basic types of data or two reference variables equal, only with the = = operator.

If the data that a variable points to is an object type, then this time involves two blocks of memory, the object itself occupies a chunk of memory (heap memory), and the variable occupies a chunk of memory, such as Objet obj = new Object (); the variable obj is a memory, and new object () is another memory, At this point, the value stored in memory for the variable obj is the first address of the memory that the object occupies (that is, the object in the variable is stored in the first address in the heap memory), and for variables that point to the object type, if you want to compare whether two variables point to the same object, That is to see if the two variables correspond to the value of the memory is equal, this time you need to use the = = operator to compare.

Second, "Equals"

The equals method is used to compare the contents of two separate objects, as compared to two people whose looks are the same, compared to the two objects that are independent of each other. For example, for the following code:
string A=new string ("foo");
string B=new string ("foo");
two new statements create two objects, and then use a, B, to point to one of the objects, which is two different objects, their first address is different, that is, the values stored in a and B are not the same, so the expression a==b will return false, and the contents of the two objects are phase- So, the expression a.equals (b) returns True.

The difference between the "= =" of the Java Foundation and the Equals method

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.