In-depth analysis of java string principles and Efficiency

Source: Internet
Author: User

  • Basic Features of java string
  1. The String class is final and cannot be inherited.
  2. The String source code is implemented through character arrays and the length cannot be changed.
  3. You can use String str = "123" to create a String object.
  4. You can concatenate two String objects to generate a new String.
  5. A String pool will be maintained during java runtime, and the String pool will save various strings generated during the program running. These strings cannot have the same content.
  • Several methods for creating strings
  1. Use the new Keyword to create String str = new String ("str ");
  2. String str = "";
  3. String str = "a", String str2 = str + "bb ";
  • Key principles of string Creation
  1. A string created in any way will be found in the string pool to see if the string exists. If it does not exist, add the string to the string pool.
  2. A string created with the new Keyword creates a new object in the stack.
  3. Create a String using String str = "aa" + "bb"; java only maintains whether it exists in the String pool.
  4. If a variable exists in the string created by String concatenation, java creates an object in the stack area.
Comparison of Different string creation locations
/***** Comparison of several string creation methods **/public static void compare () {// point to "abc" in the object pool ", do not create an object String str1 = "abc" in the stack; // point to "abc" in the object pool; do not create an object String str2 = str1 in the stack; // create an object in the stack, point to object String str3 = new String ("abc"); // point to "abc" in the object pool. The object String str4 = "a" + "bc" is not created in the stack "; string a = "c"; // create an object in the stack and point to this object String str5 = "AB" + a; // create an object in the stack, and point to the object String str6 = "AB ". concat (a); // create an object in the stack and point to this object String str7 = "AB ". concat ("c"); System. out. println (str1 = str2); // true System. out. println (str1 = str3); // false System. out. println (str1 = str4); // true System. out. println (str1 = str5); // false System. out. println (str1 = str6); // false System. out. println (str1 = str7); // false System. out. println (str3 = str4); // false System. out. println (str3 = str5); // false System. out. println (str3 = str6); // false System. out. println (str3 = str7); // false}/*** use different methods to create an empty string */public static void compareEmpty () {String str1 = new String (""); String str2 = ""; String str3 = new String (""); // intern () the method determines whether the String is in the String pool. If yes, the String str4 = "". intern (); String str5 = "" + ""; // The trim () method creates an object String str6 = "" in the stack "". trim (); String str7 = ""; String str8 = "". trim (); String str9 = "". concat (""); System. out. println (str1 = str2); // false System. out. println (str1 = str3); // false System. out. println (str1 = str4); // false System. out. println (str1 = str5); // false System. out. println (str1 = str6); // false System. out. println (str1 = str7); // false System. out. println (str1 = str8); // false System. out. println (str1 = str9); // false System. out. println (str2 = str3); // false System. out. println (str2 = str6); // false System. out. println (str2 = str8); // true System. out. println (str2 = str9); // false System. out. println (str6 = str8); // false **}
Efficiency in String operations the String splitting algorithm adopts StringTokenizer, which is more efficient than the String Split method, in String splitting, StringToken is more efficient than String concatenation in the split method. StringBuilder is more efficient than String addition or new String (). concat () method, because the string addition or concatenation method will find whether the string exists in the object pool. If it does not exist, it will be created. In this way, a large number of intermediate strings will be generated during the concatenation process, memory usage. StringBuilder is more efficient than StringBuffer, but the StringBuffer thread is secure.

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.