Differences between the equals method and the "=" Operator in Java

Source: Internet
Author: User

As the question, I believe this question has been raised by countless JavaProgramPersonnel are confused. In fact, as long as they are clear about the following points, they will be able to distinguish their differences and choose the appropriate method when appropriate:

The equals method (inherited by the string class from its superclass) is used to detect whether two objects are equal, that is, whether the content of the two objects is equal.
==: It has different functions for comparing and referencing basic data types:
Compares the basic data types. If the two values are the same, the result is true.
When comparing references, if the reference points to the same object in the memory, the result is true.

Eg:

S1 = new string ("Sony"); // creates a String object.
S1.equals ("Sony"); // returns true to compare whether the content of the S1 object is "Sony ".
S1 = "Sony" // return false to compare whether the address of the S1 object stored in the memory is the same as that of the Sony object.
// If
S1 = "Sony"; // note the difference between the assignment method and the above.
S1 = "Sony" // return true

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.