Tips for improving string and StringBuffer performance

Source: Internet
Author: User
Tags object string volatile stringbuffer
Skill | Overview of performance string and StringBuffer
A better way to create strings
The optimization caused by stranded strings
Optimization techniques for connecting strings
Optimization techniques with the help of StringBuffer initialization process
Key points Overview of string and StringBuffer
Once a Non-volatile object is created, it cannot be changed, and the Mutable object can be changed after it is created. A String object is a Non-volatile object, and the StringBuffer object is a mutable object. For better performance, you need to be cautious about using one of these two to get the most out of the situation. The following topics will be elaborated in detail. (Note: This section assumes that the reader already has a basic knowledge of Java string and StringBuffer.) ) a better way to create stringsYou can create string objects in the following ways:
1. String S1 = "Hello";
String s2 = "Hello";
2. String s3 = new String ("Hello");
String S4 = new String ("Hello"); Which of the above methods will lead to better performance? The following code fragment is used to measure the difference between the two.
Stringtest1.javapackage com.performance.string;/** This class shows the time taken for creation of
* string literals and String objects.
*/public class StringTest1 {public static void main (string[] args) {//Create String literals
Long starttime = System.currenttimemillis ();
for (int i=0;i<50000;i++) {String S1 = "Hello";
String s2 = "Hello";
Long endtime = System.currenttimemillis ();
System.out.println ("Time taken for creation of String literals:"
+ (Endtime-starttime) + "milli seconds"); Create String objects using ' new ' keyword
Long startTime1 = System.currenttimemillis ();
for (int i=0;i<50000;i++) {String s3 = new String ("Hello");
String S4 = new String ("Hello");
Long endTime1 = System.currenttimemillis ();
System.out.println ("Time taken for creation of String objects:"
+ (ENDTIME1-STARTTIME1) + "milli seconds");
}
}
The output of this code:
Time taken for creation of String literals:0 milli seconds
Time taken for creation of String objects:170 milli seconds How does the JVM handle strings?
The Java Virtual Opportunity maintains a list of internal stranded string objects (a pool of unique strings) to prevent duplicate string objects from being generated in heap memory. When the JVM loads a string literal from a class file and executes it, it checks to see if the current string already exists in the stranded string list, and if it does, it does not create a new string object, but refers to a string object that already exists. The JVM makes this check internally for string literals, but does not do this for string objects created with the New keyword. Of course you can explicitly use the String.intern () method to force the JVM to make such checks for string objects created by the New keyword.  This forces the JVM to check the internal list and use the existing string object. So the conclusion is that the JVM inherently maintains some unique string objects for literal strings, and programmers don't need to worry about literal string literals, but may be bothered by some string objects created by the new keyword, but they can use intern () method to avoid creating duplicate string objects on heap memory to improve the performance of Java operations. The next section will show you more information. The following illustration shows a situation where a string is not created using the Intern () method.
You can use the = = operator and the String.Equals () method to encode the differences mentioned above. the = = operator returns True if some references point to an identical object but do not determine whether the contents of the string object are the same, the String.Equals () method returns True if the contents of the string object being manipulated are the same. There are s1==s2 for the above code, because the S1 and S2 Two references point to the same object, and for the above code, s3.equals (S4) Returns true because the contents of the two objects are the same as "Hello". You can see this mechanism from the figure above.   Here are three separate objects that contain the same content ("Hello"), and we don't really need three separate objects-because it wastes both time and memory to run them. So how do you make sure that the string object doesn't repeat? The next topic will cover the interest in the built-in string mechanism.

[1] [2] [3] Next page



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.