String, javastring

Source: Internet
Author: User

String, javastring

Author: SmlAnt

Source: http://www.cnblogs.com/smlAnt

I. Overview

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 I add a String to the primitive type?

In the previous process-oriented language, there was no String type. to define a String, a Char [] is used. Although functions such as String operations and comparison are provided, however, it is not convenient enough and does not conform to the object-oriented approach. Therefore, in the object-oriented language, strings are also added to the queue of the primitive type;

Iii. immutable (immutable), the core feature of String)

Represents an unchangeable sequence character set, that is, once created, string encoding cannot be modified in any way. It has the following three advantages:

1. allow various operations on a string without actually changing the string;

2. Thread Synchronization will 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 had to define the String as a Sealed class during development );

4. Two methods to be overwritten: 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 finally 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 it. (therefore, the "=" operator is used to avoid a null pointer exception)

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". Therefore, the "strasse" and "sta beta e" are judged to be the same );

Note: Under normal circumstances, Equals is recommended to be used for determination, with the highest efficiency. However, if the caller of the Equals method cannot ensure that it is not null, it is recommended to use = or "XXXX ". equals (obj), If you need multi-language (international) Judgment, you can consider using Compare;

6. Interning)

String operations (such as Compare) are common operations in many programs. Such operations may result in copying multiple instances of the same string in the memory (caused by internal algorithm operations ), in order to save memory, CLR adopted a technology called "String Interning", which opened 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:

1. Define a saved string as a constant, for example, var value = "abc ";

2. Save the object to the heap: for example, var value = new string ('A ');

3. construct it as an object and store it in the detention pool (in fact, strings will be added to the resident pool by default after being defined as constants );

You can refer to the following code and the memory distribution diagram for understanding:

// Verify that string constants are loaded to metadata by default. By default, strings in metadata are loaded to the detention pool.
Var data = "abc"; // This declaration method defines this variable as a String constant and stores it in metadata.
Var a = "a"; // same as above
Var B = "B"; // same as above
Var c = "c"; // same as above
Var AB = a + B; // obtain the and B instances on the stack based on the and B addresses 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 above
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 (here it is a constant) "abc" has been stored in the detention pool;
Var empty = string. Empty; // obtains the data of the static Empty attribute from the String type object during initialization;
Var strEmpty = ""; // generates 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. Instead, it 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;

VIII. Case Analysis

1. Check whether the HashCode of the following code is the same and whether the HashCode is the same object;

Var A = "AB" + "c ";
Var B = "abc ";
 
2. Check 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. Check 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 );

 

Related Article

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.