Object comparison and value comparison for a class

Source: Internet
Author: User

1) ' = = ' and ' equals ' in the object class

The Equals method in the object class is the same as the ' = = ', and the comparison is "a reference to the memory address of the object heap stored in the stack memory", meaning that if the two variables refer to the same heap memory object, then they are ' = = ' and ' equals '.

2) "= =" and "Equals" in the String class, Integer, and so on

The ' equals ' method in String and integer classes is inherited from the object class, but they all rewrite the ' equals ' method. Thus, for these classes, ' = = ' compares the two-object references for equality, and the ' equals ' method

Compares the values of two objects for equality.

Conclusion:

For the string class, it is not possible to use ' = = ' instead of the ' equals ' method when comparing two strings.

For wrapper classes such as Integer and long, it is not possible to use ' = = ' instead of the ' equals ' method when comparing size.

For basic data types such as int and long, compare size directly with ' = = '.

Note: ' '! = ' and ' = = ' handle Similar

3) The valueof method of the integer type is special

When using the ValueOf method to convert a string type to an integer type, it is important to pay attention to the size of this value, because there is a mechanism for handling numbers in Java: -128~127 (or one)

Object comparison and value comparison for a class

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.