The difference of String,stringbuilder,stringbuffer in Java __string

Source: Internet
Author: User
Tags stringbuffer

String String constants - JDK1.0
StringBuilder String variables Thread not secure JDK1.0
StringBuffer String variables Thread Safety JDK1.5

The difference between three people

The main performance difference between a string type and a StringBuffer type is that a string is an immutable object, so each time a change is made to the string type, it is equivalent to generating a new string object, and then pointing the pointer to the new string object. If you often change the contents of a string, it is best not to use string, because each build object will have an impact on system performance, and when there are more objects in memory that have no references, the JVM's garbage collector will start to work and the program will run slower. If you are defining an object of a stringbuffer type, each operation on the contents of the string will operate on the StringBuffer object itself, rather than generating a new object. Therefore, it is generally recommended to use StringBuffer, especially where string objects are often changed. Many methods in StringBuffer can have synchronized keywords, so the thread is guaranteed to be safe, but the StringBuilder method does not have that keyword, so thread safety is not guaranteed. The StringBuilder class provides a StringBuffer-compliant API designed to be used as a simple replacement for stringbuffer, used 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. The methods of the two are basically the same.

In some special cases, a string is more efficient than stringbuffer, for example:

String S1 = "123" + "456" + "789";
StringBuffer s2 = new StringBuffer ("123"). Append ("456"). Append ("789");

The first line of code in the Java virtual machine seems to actually be:

String S1 = "123456789";

Because it was done at compile time.


the question about string = =

public static void Main (string[] args) {  
		string s1=new string ("123");            
		String S2=new string ("123");      
		System.out.println ("S1==S2:" + (S1==S2));  False       
		   
		String s3= "456";    
		String s4= "456";  
		System.out.println ("S3==S4:" + (S3==S4));  True 
	}
	
	output:
	s1==s2:false
	s3==s4:true

S1 and S2 are the memory addresses of two string objects that the Java Virtual machine has created in heap memory, although their values are the same, but = = are compared to the address, so it is false. And S3 and S4 are saved in a constant pool, which is the address of the unique reserved string object that "456" points to in the constant pool, so it is true.

public static void Main (string[] args) {  
		String S1 = "123";                                          
		String s2 = "456";                                       
		String s12 = s1 + s2;  
		String s= "123456";  
		System.out.println (s12==s); False  
	}

Variable S1,S2 stores an address in a constant pool. When the S1+S2 is executed, the JVM first creates a StringBuilder class in the heap, completes the initialization with the string object that S1 points to, and then calls the Append method to complete the merge of the string that the S2 points to. Next, call the StringBuilder ToString () method to create a string object in the heap and, finally, store the heap address of the newly generated string object in the variable S12. And the variable s stores the address of the string object corresponding to "ABCD" in the constant pool. S12 and S address must be different.


Summary

String: Situations that apply to a small number of string operations

StringBuilder: Applies to a single thread with a large number of operations in a character buffer

StringBuffer: The application of multithreading in the character buffer for a large number of operations

String constants that can be determined at compile time, it is not necessary to create a string or StringBuffer object. The "+" connection that uses string constants directly is the most efficient.












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.