Java String Overview (Next)

Source: Internet
Author: User

Summary:

The string class in Java is one of the most frequently used classes in our daily development, but it is not an easy thing to really master. In order to restore the real picture of the string class, the author divides into the first and the next two blog summarizes the string class in Java. The author from the Java memory model, combined with the JDK string class source for in-depth analysis, especially on the string class and the enjoyment of meta-mode, string constant pool, string immutability, String object creation, string and regular expression, string and clone , the difference between string, StringBuffer and StringBuilder, and so on, it is elaborated and summarized in detail, and strive to be able to make a most comprehensive and accurate introduction to the principle and use of the string class.

Friendly tips:

Should csdn Bo friends suggested that the author will be the original "Java String Review" ("The special long giant ... Haha ") divided into up and down two articles. This article mainly introduces the Java memory model and constant pool, constants and variables, string definition and foundation, the immutability of string and how to create a string object five parts, please crossing first move to the Java String Review (previous) to read, in order to better understand the content of this article. The following article (that is, this article) focuses on the relationship and differences between string constant pools, String,stringbuilder and StringBuffer three string classes, string and regular expressions, string and (deep) clones, and string summaries in five parts.

Copyright Notice:

This article original nerd Rico
Author Blog address: http://blog.csdn.net/justloveyou_/

  

up to Java String Overview (previous) ...

Six. String constant pool

1. String pool

The allocation of strings, like other object assignments, is costly in time and space. In order to improve performance and reduce memory overhead, the JVM performs some optimizations when instantiating string literals. To reduce the number of strings created in the JVM, the string class maintains a constant pool of strings, and whenever a string is created in literal form, the JVM first checks the string constant pool: If the string already exists in the pool, the instance reference in the pool is returned, and if the string is not in the pool, A string is instantiated and placed in the pool. Java can be optimized because the strings are immutable and can be shared without worrying about data conflicts. For example:

publicclass Program{    publicstaticvoidmain(String[] args)    {       "Hello";         "Hello";        System.out.print(str1 == str2);   // true    }}

An initially empty string pool, which is privately maintained by the class string. When a string is created in literal form, always first checks whether the string pool contains the object and, if so, returns directly. In addition, a string object created by the new operator does not point to any object in the string pool.

2, manually into the pool

an initially empty string pool, which is privately maintained by the class string. When the Intern method is called, if the pool already contains a string equal to this string object (as determined by the Equals (Object) method), the string in the pool is returned. Otherwise, this string object is added to the pool and a reference to this string object is returned.

It follows the following rules:
for any two strings s and T, s.intern () = = T.intern () is true if and only if S.equals (t) is true.

publicclass TestString{publicstaticvoidmain(String args[]){  "abc";  new String("abc");  String str3 = s2.intern();  System.out.println( str1 == str2 );   //false  System.out.println( str1 == str3 );   //true }}

So, for string str1 = "abc", STR1 refers to the object of the constant pool (the method area), and string str2 = new String ("abc"), str2 refers to the object in the heap, so the memory address is not the same. But because of the same content, str1 and Str3 point to the same object.

3. Summary

    • Strings created with literals are necessarily different from those created by new because they are stored in different locations: the former in the method area, the latter in the heap;

    • When we create a string object using a format such as String str = "ABC", We always think of course that we created the object str of the String class. But the object may not have been created! the only certainty is that a reference to the String class was created. As to whether the reference is pointing to a new object, it must be considered in terms of context, unless you create a new object with a prominent way through the new () method. Therefore, it is more accurate to say that we have created a reference variable to the object of the String class str, which refers to a variable that points to a string class with the value "ABC".

    • The idea of using literal literals to create strings is to enjoy meta-mode;

    • The Java compiler optimizes the string + base type/constant as a constant expression, creating a new object in the heap, or STR4 = basic type/constant + STR2, for example, "STR4 = STR2+STR3" "StringBuilder Connection" or "STR4 = basic type/constant + new String ()" is not deterministic at compile time;

      Take a look at the following code to get a deeper understanding of string:

 Public Static void Main(string[] args) {/** * Scenario One: String Pool * A string pool exists in the Java Virtual Machine (JVM), which holds many string objects;          * and can be shared and used, so it improves efficiency.          * Since the string class is final, its value cannot be changed once it is created.           * Strings are maintained by the string class, and we can call The Intern () method to access the string pool. */String S1 ="ABC";//↑ creates an object in the string poolString s2 ="ABC";//↑ string pool already exists object "ABC" (shared), so create 0 objects, accumulate an objectSystem.out.println ("S1 = = S2:"+ (S1==S2));//↑true points to the same object,System.out.println ("S1.equals (S2):"+ (S1.equals (s2)));//↑true Values Equal        //↑------------------------------------------------------ over        /** * Scenario two: About new String ("") * */String s3 =NewString ("ABC");//↑ created two objects, one stored in a string pool, one in the heap area;        //↑ also has an object reference s3 stored in the stackString S4 =NewString ("ABC");//↑ an "ABC" Object already exists in the string pool, so only one object is created in the heapSystem.out.println ("S3 = = S4:"+ (S3==S4));//↑false S3 and S4 Stacks have different addresses, pointing to different addresses of the heap area;System.out.println ("S3.equals (S4):"+ (S3.equals (S4)));//↑true S3 and S4 values are the sameSystem.out.println ("S1 = = S3:"+ (S1==S3));//↑false storage areas are different, a method area, a heap areaSystem.out.println ("S1.equals (S3):"+ (S1.equals (S3)));//↑true values are the same        //↑------------------------------------------------------ over        /** * Scenario Three: * Because the value of a constant is determined (optimized) at compile time.          * Here, "AB" and "CD" are constants, so the value of variable STR3 can be determined at compile time.          * This line of code compiles the effect equivalent to: String STR3 = "ABCD"; */String str1 ="AB"+"CD";//1 an ObjectString Str11 ="ABCD"; System.out.println ("str1 = Str11:"+ (str1 = = Str11));//↑------------------------------------------------------ over        /** * Scenario Four: * Local variable STR2,STR3 stores the address of two detained string objects (intern string objects).  * * The third line of code principle (STR2+STR3): * Runtime JVM First creates a StringBuilder class in the heap, * while initializing with the detention string object pointed to by STR2, * Then call the Append method to complete the merge of the detained string pointed to by STR3, * Then call StringBuilder's ToString () method to create a string object in the heap, * and finally the heap address of the string object that was just generated          stored in the local variable STR3.          * * While STR5 stores the address of the detention string object corresponding to "ABCD" in the string pool.          * STR4 and STR5 address of course not the same.          * * There are actually five string objects in memory: * Three detention string objects, a string object, and a StringBuilder object. */String str2 ="AB";//1 an ObjectString STR3 ="CD";//1 an ObjectString STR4 = STR2+STR3; String STR5 ="ABCD"; System.out.println ("STR4 = STR5:"+ (STR4==STR5));//False        //↑------------------------------------------------------ over        /** * Scenario Five: * The Java compiler optimizes the string + base type/constant as a direct evaluation of a constant expression. * Two strings added to the runtime, which produce new objects, are stored in the heap */String STR6 ="B"; String STR7 ="a"+ STR6; String str67 ="AB"; System.out.println ("STR7 = str67:"+ (STR7 = = str67));//↑STR6 is a variable that is parsed at run time.         FinalString str8 ="B"; String STR9 ="a"+ str8; String str89 ="AB"; System.out.println ("STR9 = str89:"+ (STR9 = = str89));//↑STR8 is a constant variable and the compilation period is optimized        //↑------------------------------------------------------ over}
Seven. String (string constant), StringBuilder [string variable (non-thread safe)] and stringbuffer[string variable (thread safe)]

1.String and StringBuilder

Briefly, the main performance difference between the string type and the StringBuilder type is that the string is an immutable object. Therefore, each time a change is made to a string type, it is actually equivalent to generating a new string object and then pointing the pointer to the new string object. Therefore, it is best not to use string to change the content of the strings, because each generation of objects will have an impact on the system performance, especially when there is no reference object in memory, the JVM's GC will start to work, the speed will be quite slow . If you use the StringBuilder class, the result will be different, and each result will operate on the StringBuilder object itself instead of generating a new object and changing the object reference. Therefore, in general, it is recommended to use StringBuilder, especially if the string object is often changed .

In some special cases, string concatenation can be determined directly by the JVM in the compiler. So, at this point in speed, StringBuilder does not take any advantage.

String S1 = “This is only a” + “ simple” + “ test”;      //编译期完成字符串常量的串联,相当于“This is only a simple test”new StringBuilder(“This is only a”).append(“simple”).append(“ test”);

For

String S1 = “This is only a” + “ simple” + “test”;  

is actually:

It is important to note that if the following is the case, its internal implementation is the first new StringBuilder, and then calls its Append method connection, the efficiency is lower.

String S2 = “This is only a”;String S3 = “ simple”;String S4 = “ test”;String S1 = S2 +S3 + S4;

So, in most cases, in terms of efficiency: StringBuilder > String.

2.StringBuffer and StringBuilder

First,both StringBuffer and StringBuilder inherit from Abstractstringbuilder in the implementation of the JDK. Abstractstringbuilder is implemented by using a char array in Abstractstringbuilder to hold the string that needs to be append, and the char array has an initial size, When the string length of append exceeds the current char array capacity, the char array is dynamically extended, that is, re-requesting a larger memory space, and then copying the current char array to a new location because the overhead of reallocating memory and copying is large, So every time you re-apply for memory space is the way to request more than the current need of memory space, here is 2 times.


StringBuffer starts at JDK 1.0
StringBuilder starts at JDK 1.5

Starting at JDK 1.5, a connection operation (+) with string variables (non-string literals) is used internally by the JVM
StringBuilder, which was implemented by StringBuffer.

  Java.lang.StringBuffer is a thread-safe variable character sequence. A string-like buffer, but cannot be modified. Although it contains a specific sequence of characters at any point in time, some method calls can change the length and content of the sequence.

  Java.lang.StringBuilder is also a variable sequence of characters that is new to JDK 5.0. This class provides an API that is compatible with StringBuffer, that is, the methods and functions in StringBuffer and StringBuilder are completely equivalent, but do not guarantee synchronization. This class is designed to be used as a simple replacement for stringbuffer, which is common when a string buffer is used by a single thread. If possible, it is recommended that this class be preferred because, in most implementations, it is faster than StringBuffer.

Therefore, under single thread, StringBuilder is preferred.

For a summary of the three uses:

    • single-threaded operation string buffers operate a large number of data StringBuilder, multi-threaded operation string Buffer under the operation of a large number of data stringbuffer;

    • Use string if the data you are manipulating does not change very much (string is immutable) or string constants are glued (compile-time optimizations are string constants) or operations are simpler (excluding loops, etc.);

      For example:

"b” + "c”; String s1  =  "a"; String s2  =  "b"; String s3  =  "c"; String s4  =   s1  +  s2  +  s3;

Analysis: The creation of the variable S is equivalent to String s = "abc"; The compiler is optimized by the example above, where only one object is created. It can also be known by the above example; S4 cannot be optimized at compile time, and its object creation is equivalent to:

    new StringBuffer();    temp.append(s1).append(s2).append(s3);    String s = temp.toString();

From the above analysis results, it is not difficult to infer that the string using the Join operator (+) inefficiency reason analysis, such as the code:

publicclass Test {     publicstaticvoidmain(String args[]) {         null;             for(int0100; i++) {                 "a";             }     } }

Every time you do it, a StringBuilder object is created and then thrown away after append. The next time the loop arrives, it re-StringBuilder the object and then append the string so that it loops until the end. If we use the StringBuilder object directly for Append, we can save N-1 time to create and destroy objects. Therefore, the application of string connection in the loop is generally done by using the Stringbulider object for append operation.

Eight. Strings and regular Expressions: matching, substitution, and validation
    • Regular Expressions: Use a string to describe a feature, and then verify that another string conforms to that feature. Using regular expressions, we are able to construct complex text patterns in a programmatic way, and search for the input strings;

    • Java Escape (\) and regular expression escape (\\);

    • Use Pattern and Matcher to construct powerful regular expression objects.

Nine. String and (deep) cloning

1. Basic

    • Target: Make a copy of an object

    • Type: Shallow clone; deep clone

    • Clone & copy
      Assuming there's an EM Ployee object, Employee Tobby = new Employee ("Cmtobby",), usually we will have such an assignment Employee cindyelf=tobby, It's just a simple copy. Reference , cindyelf and Tobby all point to the same object in memory, so that an operation cindyelf or tobby can affect each other. For example, if we change the value of the salary field through the Cindyelf.raisesalary () method, then tobby through the Getsalary () method is the value of the modified Salary field, which is obviously not what we would like to see. We want to get an exact copy of the Tobby, while the two do not affect each other, and we can use clone to meet our needs. Employee Cindy=tobby.clone (), a new employee object is generated and has the same property values and methods as Tobby.

    • Shallow clone & Deep clone
      Clone How is it done? object does not know anything about an object when it is being Clone, it simply executes the domain-to-domain copy, which is shallow Clone . So, here's the problem. For employee, for example, there is a domain hireday is not a basic type of variable, but a reference variable, after cloning will produce a new date type of reference, It points to the same date object as the corresponding field in the original object, so that the clone class shares a portion of the information with the original class, which is obviously unfavorable, as shown in the procedure:

      This time we need to do deep clone, for those non-basic types of domain Special handling, such as the hireday in this example. We can redefine the Clone method and do special processing for hireday, as shown in the following code:

class Employee implements Cloneable  {       publicclonethrows CloneNotSupportedException {         super.clone();         cloned.hireDay = (Date) hireDay.clone() ;   //public class Date implements java.io.Serializable, Cloneable, Comparable<Date>       return cloned;      }  }  

  Therefore, object does not know what to do when it implements Clone on an object, it simply executes domain-to-domain copy. There is no problem with eight basic types of clones, but when cloning an object, it simply clones its reference. As a result, the cloned object and the original object share the same object member variable, so a deep clone is proposed: After the whole object is simply cloned, it is necessary to clone the reference variable and update it to the shallow clone object.

2. Protection mechanism of Clone () method

In Object Clone () is declared as protected , this is a certain reason, in the case of the employee class, through the declaration of protected, you can guarantee that only the employee class (and its subclasses) inside to "clone" The Employee object.

3. Use of the Clone () method

The use of the Clone () method is relatively simple, note the following points:

    • When to use shallow clone, when do I use deep clone?
      This is mainly about the nature of the domain of the specific object, the basic type or the reference type
    • The class to which the object calling the Clone () method belongs must implements the Clonable interface, or it will be thrown when the Clone method is called Clonenotsupportedexception
    • Understand the specificity of String in cloning

      String clones its reference only when it is cloned.

      Strangely, when the cloned String object is modified, its original object has not changed. The reason is:string is in memory can not be changed objects , such as in for a large loop is not recommended to use the + method to cobble together a string, each use of the + will be a new allocation of memory, not the original modification, the original does not point to its reference, will be recycled. So cloning is equivalent to 1 string memory space has two references, when modifying one of these values, a new memory is allocated to save the new value, this reference points to the new memory space, the original string because there is still a reference to him, so will not be recycled, so, although it is a copy of the reference, However, when the value is modified, the value of the copied object is not changed.

      So in many cases, we can do the same thing with the basic type when the String is in clone.

10. String Summary
    • When you create a string in literal form, you do not necessarily create an object, but the object it obtains must be in a string constant pool;
    • Use new String: Be sure to create an object, or even create two objects;
    • The String object is immutable;
    • StringBuilder and StringBuffer have a common parent class, with the same API, respectively, for single-threaded and multi-threaded environments;
    • What is the method used for string comparisons, and how is the internal implementation?

      Use the Equals method: first compare whether the reference is the same (whether it is the same object), check for the same type (str instanceof String), and finally compare the consistency of the content (the values or contents of the individual member variables of the String are the same). This also applies to eight wrapper classes such as Integer.

More about the literal introduction please visit my blog "Java native type and wrapper type depth analysis";

For more information about the enjoy Meta model, please visit my blog, "in-depth understanding of the meta-mode".

references

JVM memory model and garbage collection algorithm
Deep understanding of Java:string
Special string Types in Java
Why is a string in Java immutable? –string Source Code Analysis
The difference between String,stringbuffer and StringBuilder and the application scenario
Anatomy of the Clone method in Java
The particularity of string in Java cloning
A detailed explanation of the Java heap, stack and Chang, and related string (Classic in classic)
What is a string constant pool?
Heap, stack, and constant pools in Java

1

1

1

1

Java String Overview (Next)

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.