[Java object-oriented BASICS (II)] elaborate on String, StringBuffer, StringBuilder, stringbuffer

Source: Internet
Author: User

[Java object-oriented BASICS (II)] elaborate on String, StringBuffer, StringBuilder, stringbuffer
[Meow "'s Android path] [BASICS (II)] [Java object-oriented Basics] I will elaborate on String, StringBuffer, StringBuilder 1, and StringString as a final class in Java, it is mainly used for string processing. 1.1 immutable strings are immutable. Each modification will generate a new String object instance. Example:

1 // a "Hello" String instance is created in the heap, and the address is assigned to the a2 String a = new String ("Hello") object "); 3 // create another "World" String instance in the heap and assign the new address to the object a4 a = new String ("World ");

After the above operations, the string "World" is not modified in the space of the original string "Hello", but is re-opened to save the space "World", and then assigned the address of the new space to

1.2 String constant pool the String constant is saved in the Java. class file constant pool. It has been determined during the compilation period and is loaded by JVM during the runtime. Java ensures that a string has only one copy in the constant pool. Example:
1 // create reference a, add the "Hello" String in the constant pool, and assign the address of the "Hello" String to a 2 String a = "Hello "; 3 // create reference B, search for the "Hello" String in the constant pool, and assign the address of the "Hello" String to B 4 String B = "Hello "; 5 // create reference c and search for the spliced "Hello" string in the constant pool, assign the address of the "Hello" String to c 6 String c = "He" + "llo"; 7 // at this time, the addresses referenced by objects a, B, and c are the "Hello" addresses in the constant pool. Therefore, the following comparison returns true 8 System. out. println (a = B); // true 9 System. out. println (a = c); // true10 11 // but the following method does not save 12 // create reference d in the constant pool, open the space storage String "Hello" in the heap and assign the space address to d13 String d = new String ("Hello"); 14 15 // at this time, d. The corresponding address will be the "Hello" string address in the heap, while the address of "Hello" in the uncommon pool is 16 System. out. println (a = d); // false

 

1.3 The String constant pool of the intern method is not fixed. It is scalable and can be expanded using the String. intern () method. The intern () method is used to add the value of the current String object to the constant pool. If the constant pool already contains this string, the address of the string in the constant pool is returned, if this string does not exist in the constant pool, it is appended to the constant pool and the address after it is appended to the constant pool is returned. Let's take a look at the example below:
1 // execute the extension, but the address that references d is not modified. Therefore, the comparison between a and d is still false2 d. intern (); 3 System. out. println (a = d); // false4 5 // execute the extension and assign the new address to d. Because the new address points to the "Hello" string that already exists in the constant pool, so the reference address of a and d is the same 6 d = d. intern (); 7 System. out. println (a = d); // true

 

1.4 although String is a class and belongs to the reference type, as mentioned above, String is an immutable String object. Therefore, when passed as a parameter, it is different from other reference types: if the parameter is modified in the method body, its original reference value will not change. Example:
 1 static void testParamTrans() { 2     String param = "Hello World"; 3     User user = new User(0, "Nodin", 23); 4      5     System.out.println("Before-------------"); 6     System.out.printf("param is %s, user.name is %s\n", param, user.getName()); 7   8     change(param, user); 9     10     System.out.println("After------------");11     System.out.printf("param is %s, user.name is %s\n", param, user.getName());12 }13  14 static void change(String param, User user) {15     param = "New World";16     user.setName("New Name");17 }

 

Output result: Before -------------
Param is Hello World, user. name is Nodin
After ------------- param is Hello World, user. name is New Name, we can see that the value of the variable param has not changed before and After the change method is modified. 1.5 for more information about the strings, see [meow "'s Android path] [more] About = and equals 2. StringBuffer is a thread-safe string sequence, similar to a String buffer, strings in the buffer can be modified, but the buffer itself cannot be modified. The reason for thread security is that the buffer zone of StringBuffer uses the synchronized keyword for Synchronous Control in the face of multi-threaded requests to ensure that only one thread accesses the buffer zone at the same time. We recommend that you use String concatenation, especially in a thread-safe environment. Common methods include append, insert, length, toString, delete, and deleteCharAt. 3. StringBuilder is a simplified version of StringBuffer, Which is lighter than StringBuffer. The main difference is that synchronized keyword control is removed, that is, non-thread security. We recommend that you use String concatenation, especially single-threaded String concatenation. The common method is the same as that of StringBuffer. 4. Differences between String, StringBuffer, and StringBuilder: String, StringBuffer, and StringBuilder are all final classes that cannot be inherited and are serialized at the same time. StringBuffer and StringBuilder inherit from AbstractStringBuilder at the same time; StringBuffer thread security, stringBuilder is non-thread-safe. String is an unchangeable String object, while StringBuffer and StringBuilder correspond to the String buffer. Applicable environment and efficiency: String: used for simple String operations, A large number of string operations, especially the splicing efficiency is extremely low; StringBuffer: used for String concatenation, especially multi-threaded String concatenation, high efficiency; StringBuilder: used for single-threaded String concatenation, the highest efficiency. This article is original from Nodin. For more information, see the source! Http://www.cnblogs.com/monodin/p/3841188.html
Use Java StringBuffer or StringBuilder to operate strings

Public class Du02 {

Public static void main (String [] args ){

// 1)
StringBuilder sb = new StringBuilder ("I am a student, I am at ccit ");
System. out. println (sb. toString ());

// 2)
Sb. insert (sb. indexOf (",") + 1, "this is a test ,");
System. out. println (sb. toString ());

// 3)
Sb = new StringBuilder (sb. toString (). replaceAll ("ccit", "CCIT "));
System. out. println (sb. toString ());

// 4)
Sb = new StringBuilder (sb. toString (). replaceAll ("am ",""));
System. out. println (sb. toString ());
}

}

--------- Testing
I am a student, I am at ccit
I am a student, this is a test, I am at ccit
I am a student, this is a test, I am at CCIT
I a student, this is a test, I at CCIT

Differences between String, StringBuffer, and StringBuilder in Java

Based on the comparison above, we found that the mechanism in C # is no different from that in Java, the difference is the underlying language problem. In String s = Java, the internal structure of StringBuffer is an array and the character

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.