String-Explanation of interest

Source: Internet
Author: User

I. Summary String is one of the most commonly used primitive types, although we often use it and are very familiar with it; however, many friends only know how to define, use, or use StringBuilder to Efficiently build a string, but how many friends are interested in learning the secrets behind them "? Ii. Why do we add a String to the primitive type? In the previous process-oriented language, the String type is not. To define a String, a Char [] is used. although functions such as operations and comparisons on strings are provided, they are not convenient enough and do not conform to the object-oriented approach. Therefore, in the object-oriented language, strings are added to the queue of the primitive type; 3. immutable (immutable), the core feature of String, represents an unchangeable sequence character set. Once created, String encoding cannot be modified in any way. It has the following three advantages: 1. allows you to perform various operations on a string without actually changing the string. 2. thread Synchronization does not occur when a string is operated or accessed; 3. based on performance considerations, the String type is closely integrated with the CLR. the CLR knows how to layout the fields defined in the String type, and the CLR will directly access it, therefore, we have to define the String as a Sealed class during development. 4. Rewrite the two methods GetHashCode and Equals 1.. The GetHashCode method is rewritten to satisfy the judgment of two strings. 2. the Equals method is rewritten. The Equals method still calls the GetHashCode method to judge. 5. "=", Equals, and Compare. Which of the following is better for string determination? 1. string is used to reload the "=" operator. After a null value is determined by the internal implementation, Equals is called to judge the operation. (therefore, the "=" operator is used to avoid NULL pointer exceptions.) 2. string Equals method, because when the Equals method is called, it is compared directly by returning HashCode, with the highest efficiency. It is possible that the object is null, and a null pointer exception may be thrown, so pay attention to it when using it; 3. compare method of String: This method is a static method. The internal implementation is to first determine whether the length of the String is equal. If the length is not equal, the result is directly returned. If the length is equal, the results are judged by characters one by one. If the CultureInfo in the method is not empty, the results will be expanded one by one during the judgment process (the language problem is involved here, if German is used, "Beta" is expanded to "ss", so the "strasse" and "sta beta e" are judged to be the same). Note: if, in general, it is recommended to use Equals for determination, with the highest efficiency. However, if the caller of the Equals method cannot be sure whether it is not null, we recommend using = or "XXXX ". equals (obj), If you need multi-language (international) Judgment, you can consider using Compare; 6. Interning string operations (such as Compare) this operation may cause multiple instances in the memory to copy the same string (caused by internal algorithm operations). In order to save the memory, CLR adopts a technology called String Interning, which opens up a space named "detention pool" for storing strings, the detention pool loads metadata to the "detention pool" by default during program initialization, and will not be affected by the garbage collector, resources in the "detention pool" will be released only when the program is closed. 7. storage methods are roughly divided into the following three types: 1. define a string that is saved as a constant, for example, var value = "abc"; 2. save the object to the heap: for example, var value = new string ('A'); 3. constructed as an object and stored in the detention pool (strings are also added to the resident pool by default after being defined as constants). You can refer to the following code and Memory Distribution Diagram for understanding: // verify that the String constant is loaded to metadata by default. By default, the string in the metadata is loaded to the detention pool var data = "abc"; // This declaration method, the variable is defined as a String constant and then saved to the metadata var a = "a"; // same as var B = "B"; // same as var c = "c "; // same as var AB = a + B; // obtain instances a and B on the stack based on the address a and B on the thread stack, then, calculate the results of the two instances and generate a new object. Finally, the new object address is assigned to the AB variable var abc = a + B + c; // same as var abResult = string. isInterned (AB); // The returned result is null, that is, the string "AB" is not stored in the detention pool; var abcResult = string. isInterned (abc); // The returned result is "abc", that is, the string (constant) "abc" has been stored in the detention pool; var empty = string. empty; // obtain the data of the static Empty attribute from the String type object during initialization; var strEmpty = ""; // generate a String object with null values; var a1 = "abc"; var a2 = string. isInterned (a1); var result = object. referenceEquals (a1, a2); Note: by default, it is not loaded from the detention pool, but directly uses the special instructions of ldstr to obtain the string "abc ", however, it can be confirmed that the strings in the "metadata" and "detention pool" are used as objects. 8. Case Analysis 1. whether the HashCode of the following code is the same, whether they are the same object; var A = "AB" + "c"; var B = "abc"; 2. whether the HashCode of the following code is the same and whether they are the same object: var A = Console. readLine (); // enter "abc" var B = Console. readLine (); // enter "abc" 3. whether the HashCode of the following code is the same and whether they are the same object: var A = Console. readLine (); // enter "abc" var B = Console. readLine (); // enter "abc" var A = string. intern (B );

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.