Equals () and = = Comparison of string

Source: Internet
Author: User

Equals () and = = Comparison of string

Chang (Constant Pool): Refers to some data that is determined at compile time and is saved in the compiled. class file. The JVM virtual machine maintains a constant pool for each mounted type. A constant pool is an ordered set of constants used by that type, including direct constants (String,integer and floating point constants), and symbolic references to other types, fields, and methods. For a string constant, its value is in a constant pool. The Chang in the JVM exists as a table in memory, and for string types there is a fixed-length constant_string_info table for storing literal string values, note that the table stores only literal string values and does not store symbol references.

1, String s = "abc";

Create Process Analysis : When the class file is loaded into memory by the JVM, the JVM creates a string pool (string buffer). When executing string s = "abc", the JVM first looks in the string pool for the existence of the string object "ABC" (How do I see it?). As determined by the Equals () method), if the object already exists, you do not have to create a new string Object "ABC", use the object "ABC" that already exists in the string pool, and then point the reference s to that object, or if the object does not exist, first in string Create a new String object "ABC" in the pool, and then point the reference s to the new object created in the string pool.

Note: When you create a string object using the string constant quotation mark, it is determined at compile time that the object is stored in the string pool. Therefore, string s = "abc" only creates an object in the string pool at compile time.

2, String s = new String ("abc");

Create Process Analysis : When executing string s = new String ("abc"), the JVM first looks in the string pool for the existence of the string object "ABC", and if the object does not exist, first in string Create a new String object "ABC" in the pool, and then execute new string ("abc") construction method, create a new string object "ABC" in the Heap (all new objects are placed in the heap), and reference s to the new object created in the heap If the object already exists, you do not have to create a new string Object "ABC", use the object "ABC" that already exists in the string pool, and then execute the new string ("abc") construction method, and in the heap, create another "abc" of the Strings object. The reference S is pointed to the new object created in the heap.

Note: When you create a string object with the new string (""), a new object is created at run time to be stored in the heap. Therefore, when new string ("abc") creates a string object, 2 objects are created, the compilation period is created in a string pool, and one is created in the runtime heap.

Example code:

  

String a= "Hello";//a constant pool of strings to be stored during compilationString b=NewString ("Hello");//is not created during run time        FinalString c= "Hello";//A string constant pool has been stored during compilationString d= "Hel"; String e= "Lo"; String F=NewString ("Lo"); System.out.println (A==B);//falseSystem.out.println (A.equals (b));//trueSystem.out.println (A==C);//trueSystem.out.println (A.equals (c));//trueSystem.out.println (B==C);//falseSystem.out.println (B.equals (c));//trueSystem.out.println ("******split*****"); System.out.println (A= = (d+f));//falseSystem.out.println (A.equals (d+f));//trueSystem.out.println (c== (d+f));//falseSystem.out.println (C.equals (d+f));//true

Note the code:

1, for has + number, and has the reference variable:

  

String S1 = "ABC"; String S2= "Def"; String S3= "ABCdef"; String S4= "abc" + "Def"; String S5= S1 + "Def"; String S6= "ABC" +S2; String S7= S1 +S2; System.out.println (S3==S4); System.out.println (S3==S5); System.out.println (S3==S6); System.out.println (S3==S7); The results of the operation are as follows:true     false     false     false   
Result Description: JVM for strings that have string referencesThe "+" connection, while the referenced value cannot be determined during the program compilation period, i.e. S1 +"Def" cannot be optimized by the compiler, but is dynamically allocated during the program's run time and assigns the new address after the connection to S5. However, the exception to the mandatory declaration is final, as follows:FinalString S1 = "ABC"; FinalString s2 = "Def"; String S3= "ABCdef"; String S4= "abc" + "Def"; String S5= S1 + "Def"; String S6= "ABC" +S2; String S7= S1 +S2; System.out.println (S3==S4); System.out.println (S3==S5); System.out.println (S3==S6); System.out.println (S3= = s7);

The results of the operation are as follows:
True
True
True
True

Result Description
: The difference between routine 3 and routine 4 and routine 2 is that routine 3 has a final decoration before the string S1, and routine 4 is final decorated before the string S1 and S2. For a final modified variable, it is stored in its own constant pool or embedded in its byte stream at compile time by a local copy that is resolved to a constant value. So the S1 + "Def" and "abc" + "def" effects are the same at this point. Next two strings with references are concatenated, and the JVM performs the same processing. Therefore, the result of the above program three is true.

2. For method calls Public Static voidMain (String args[]) {string S1= "ABC"; FinalString s2 =getdef (); String S3= "ABCdef"; String S4= "ABC" +S2; String S5= S1 +S2; System.out.println (S3==S4); System.out.println (S3==S5); }  Private StaticString getdef () {return"Def"; The results of the program run are as follows:false falseThe result is that the JVM assigns a value to a string reference to a method call, the value of the reference to the string cannot be determined at compile time, and the return value "Def" and "abc" of the method are dynamically concatenated and assigned a new address assignment to S4, only after the program has run the calling method. So the result of the above procedure is false. 

String S=new string ("xyz") +new string ("xyz"), creating a total of four objects

For the first new String ("xyz"), you need to store XYZ in Stringpool to create an object, and the constructor creates an object

For the second new String ("XYZ"), the constructor needs to create an object

The last two strings added will then be stringpool again to create an object

Thus, a total of 4 objects were created

Equals () and = = Comparison of string

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.