= = and Equal in Java

Source: Internet
Author: User

For beginners, the = = and equal in Java tend to understand the logic easily confusing. Here is a simple record of the focus of understanding the two.

  • = = compares the memory addresses with the same
  • Equal need a few points of understanding
    • Equal in the object class: is also used = = to compare, so it is also the address of the comparison
    • Equal in other classes generally have the equal method overridden in object, depending on the specific class. Here is a detailed analysis of the equal method in the String class (because I understood the confusion-.-)
        •   

          First understand the string buffer pool--you can point a reference to the same string content to the same in-memory content

          • eg1:string a= "Hello"; String b = "Hello"; A, b refers to the same address because the virtual machine is assigned

          • eg2:string a= "Hello"; String B=new string ("Hello"); b requires the allocation of new memory, so A/a reference address is different
          • EG3:String a= "Hello"; String B=new string ("Hello"); B=b.intern ();//b invokes the method in Java.lang.String, which searches the string buffer for a reference to "Hello" in the pool, and returns the reference address if there is one. So here A, B refers to the same address
        • The equal method of string
          • Returns true for the same string contents, and False for different returns. See the API for details, as shown in the following example:

The understanding of = = and equal first written here, but also give themselves a chance to understand ~

= = and Equal in Java

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.