==, Equal

Source: Internet
Author: User
=: 1. Compare whether the operands at both ends of the operator are the same object. 2. The operands at both ends must be of the same type (between parent and child classes) before compilation. 3. Compare the address. If it is a comparison of specific Arabic numbers, the value is true if it is equal, for example: int A = 10 and long B = 10l and double C = 10.0 are both the same (true), because they all point to the heap equals with the address 10: 1. Compare whether the content of the two objects is the same. String S = "abce" is a very special form. It is essentially different from new. It is the only way in Java to generate objects without the need for new. Value assignment in the form of string S = "abce"; in Java is called a direct volume, which is placed in the constant pool rather than in the compression heap like new. This form of string is in the JVM's internal string detention. That is, when such a string is declared, the JVM will first find an object with no value of "ABCD" in the constant pool, if yes, it will be assigned to the current reference. that is, the original reference and the current reference are directed to the same object. If not, create a new "ABCD" in the constant pool. If the next time there is a string S1 = "ABCD "; it also points S1 to the "ABCD" object, that is, the string declared in this form, as long as the value is equal, any multiple references point to the same object. string S = new string ("ABCD"); Same as any other object. each call generates an object as long as they are called. It can also be understood as follows: String STR = "hello"; first, check whether there is a "hello" object in the memory. If yes, let STR point to the "hello ". if there is no "hello" in the memory, create a new object and save "hello ". string STR = new string ("hello") is to create a new object to save "hello" no matter whether there is a "hello" object in the memory. For details, refer to the following code: copy the public class test1 {public static void main (string [] ARGs) {string a = new string ("AB "); // A is a reference string B = new string ("AB"); // B is another reference, and the object content is the same as string AA = "AB "; // put it in the constant pool string BB = "AB"; // search for if (AA = bb) from the constant pool // true system. out. println ("AA = BB"); if (a = B) // false, not the same object system. out. println ("A = B"); if (. equals (B) // true system. out. println ("aeqb"); If (42 = 42.0) {// true system. out. println ("true") ;}} copy the code

 

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.