What is the difference between "= =" and the Equals method?

Source: Internet
Author: User

(Make one thing clear, and then another, so that the difference will come out naturally, and it's hard to say if you mix it up)

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 it involves two blocks of memory, the object itself occupies a chunk of memory (heap memory), and the variable takes up a chunk of memory, for example Objet obj = new Object (); variables obj is a memory, new Object () is another memory, at this point, the variable obj The value stored in the corresponding memory is the first address of the block of memory that the object occupies. For variables that point to the object type, if you want to compare whether the two variables point to the same object, that is, to see if the values in memory for the two variables are equal, then you need to compare them with the = = operator .

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 statement Create two objects and then use a,b These two variables point to one of the objects, which is two different objects, Their first address is different, that is a b The values stored in a==b will return false a.equals (b) will return true

in real-world development, we often have to compare whether the string content that is passed is, for example, String input = ...; Input.equals ("Quit"), many people do not pay attention to use = = To compare, this is wrong, casually from the Internet to find a few project real-life teaching video to see, there are a lot of such errors. Remember that the comparison of strings is basically using the equals method.

if a class does not define its own equals method, it inherits the equals method of the object class , andthe object class the implementation code for the Equals method is as follows :

Boolean equals (Object o) {

return this==o;

}

This means that if a class does not have its own definition of the Equals method, its default equals Method (inherited from the Object Class) is using the = = operator. It is also in comparison to whether the object pointed to by two variables is the same object, when using equals and using = = will get the same result , if the comparison is two separate objects will always return false. If you are writing a class that wants to compare the contents of the two instance objects created by the class, then you must override the Equals method and write your own code to determine at what time two objects can be considered The content is the same .

What is the difference between "= =" 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.