Summary of Java = = and Equals method

Source: Internet
Author: User

1  Public classEqualstest {2 3      Public Static voidMain (string[] args) {4String S1 = "ABC";5String s2 = "abc";6String s3 =NewString ("ABC");7String S4 =NewString ("ABC");8         9         /*Ten * First to be clear: = = Comparison is the object reference, that is, the address in memory; One * equals compares the content in the reference; A          *  - * Also note: equals this method is the method of the object base class, the original source code is actually the same as the = = effect, so if the subsequent does not overwrite this method, even if - * Equals is also a comparison of the address references rather than the contents of them: The comparison of string strings, which we typically use directly, has overridden the Equals method to compare content, the * So it can be used directly.  -          */ -          -System.out.println ("S1==S2:" + (S1==S2));//result is true +         /* - * Analysis: S1==s2 Returns True, indicating that S1 and S2 point to the same memory address; + * When the string "ABC" assigns a value to the created string reference, Java will first look in the constant pool to see if it already has the constant object, and if so, give the address of the constant to the reference object directly; A * when "ABC" first enters the constant pool, creates the S1 object and will be the S1 object reference to the "ABC" address, and when the second constant "ABC" enters, the virtual opportunity first looks at whether the constant pool has the same at * Constant, at this time found already exist, so directly to the S2 object reference to the "ABC" address, so S1 in fact, and S2 point is the same address; -          */ -System.out.println ("S1 equals s2:" +s1.equals (S2));//result is true -         /* - * The String class overrides the Equals method, compares the referenced content, and returns true as two content; -          */ inSystem.out.println ("S1==S3:" + (S1==S3));//The result is false -         /* to * String S1 = "abc"; + * String s3 = new String ("abc"), this is the two ways that the string class provides the creation of an object instance, because the string object is immutable and is created in memory. - * So the first way to think of "ABC" as a constant, when created directly in the method area of the constant pool, should usually be created in this way, will first in the constant pool search whether there is already such a constant, if there is the * The address is assigned directly to the reference, so this method is the same regardless of whether it is an address reference or an applied content.  * * While the second method is a new object and is initialized directly, the new String ("ABC") object is placed directly into the heap memory to open up space, and the reference address is assigned to the reference variable s3, it can be described in general $ * The object reference created by new is a newly opened memory space, the comparison reference must be Fasle, if the contents of the two reference address are the same, then equals is true;Panax Notoginseng          */ -System.out.println ("S1 equals S3:" +s1.equals (S3));//result is true the         /* + * Compare references to address contents, same A          */ theSystem.out.println ("S3==S4:" + (S3==S4));//The result is false +         /* - * Compare object references, which belong to different references, false $          */ $System.out.println ("S3 equals S4:" +s3.equals (S4));//result is true -         /* - * The reference address is different, but the reference points to the same content, true the          */ -     }Wuyi  the}

Summary of Java = = and 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.