Java basic integer and integer comparison

Source: Internet
Author: User

The operator is used to compare whether the values of two variables are equal, that is, to compare whether the values stored in the memory corresponding to the variables are the same, to compare two basic types of data or whether two referenced variables are equal, you can only use the = Operator. <Br> If the data that a variable points to is of the object type, two memory blocks are involved. The object occupies one memory (heap memory) and the variable also occupies one memory, for example, objet OBJ = new object (); variable obj is a memory, and new object () is another memory, the value stored in the memory corresponding to the variable obj is the first address of the memory occupied by the object. For variables pointing to the object type, if you want to compare whether two variables point to the same object, it depends on whether the values in the memory corresponding to the two variables are equal, in this case, we need to use the = Operator for comparison. <Br> The equals method is used to compare whether the content of two independent objects is the same. It is like comparing two objects with the same appearance. The two objects are independent. For example, for the following code: <br> string a = new string ("foo"); <br> string B = new string ("foo "); <br> two new statements create two objects, and use the and B variables to point to one of them. These two variables are two different objects whose first addresses are different, that is, the values stored in expression A and expression B are different. Therefore, expression A = B returns false, and the content of the two objects is the same. Therefore, expression. equals (B) returns true. <Br> in actual development, we often need to compare the passed string content, for example, string input = & #8230; input. equals (& #8220; quit & #8221;), many people do not pay attention to it and use = for comparison. This is wrong. You can find some practical teaching videos from the Internet, there are a lot of such errors. Remember, strings are basically compared using the equals method. <Br> If a class does not define its own equals method, It inherits the equals method of the object class. The implementation code of the equals method of the object class is as follows: <br> Boolean equals (Object O) {<br> return this = O; <br >}< br> This indicates that if a class does not define its own equals method, its default equals method (inherited from the object class) is to use the = operator, and is also comparing whether the objects pointed to by two variables are the same object, in this case, equals and ==get the same result. If two independent objects are compared, false is always returned. If you want to write a class to compare whether the content of the two instance objects created by the class is the same, you must overwrite the equals method, it is up to you to write your own code to determine when the content of the two objects is the same.

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.