Fully explain the relationship between StringBuilder StringBuffer and String classes in Java

  • 2020-04-01 01:20:19
  • OfStack

1. The String class

The value of a String is immutable, which results in new String objects being generated every time you manipulate a String, which is not only inefficient, but also wastes a lot of limited memory space.
String a = "a". // suppose a points to address 0x0001
A = "b". // after re-assignment,a points to address 0x0002, but the "a" saved in address 0x0001 still exists, but is no longer pointed to by a,a has pointed to other addresses.
So String operations are all about changing the assigned address rather than changing the value.

2. StringBuffer is a mutable class, a thread-safe string manipulation class, and any operation on the string it points to does not result in a new object. Each StringBuffer object has a certain buffer capacity. When the string size does not exceed the capacity, no new capacity is assigned, and when the string size exceeds the capacity, the capacity is automatically increased.

StringBuffer buf = new StringBuffer (); // allocates a 16-byte character buffer
StringBuffer buf = new StringBuffer (512); // allocates a 512-byte character buffer
StringBuffer buf=new StringBuffer("this is a test")// the StringBuffer buf=new StringBuffer("this is a test")// the StringBuffer buf=new StringBuffer("this is a test")

3. The StringBuffer
The StringBuffer and StringBuilder classes are basically similar in functionality, with the main difference being that the StringBuffer class's methods are multithreaded and safe, while StringBuilder is not thread-safe, and the StringBuilder class is slightly faster. The StringBuffer and StringBuilder classes should be used for strings that frequently change values.

4. Thread safety
StringBuffer thread safety
The StringBuilder thread is not safe

Speed of 5.
In general, the speed goes from fast to slow :StringBuilder> StringBuffer> String, this comparison is relative, not absolute.

6. Summary
(1). If you want to operate a small amount of data, use = String
(2). Single thread operation string buffer to operate a large amount of data = StringBuilder
(3). Multithreaded operation of a large number of data in the StringBuffer = StringBuffer

Here are the code and the demo:

 
public class TestCharacter { 
final static int time = 50000; //cycles

public TestCharacter(){ 

} 
public void test(String s){ 
long begin = System.currentTimeMillis(); 
for(int i=0; i<time; i++){ 
s +=  " add " ; 
} 
long over = System.currentTimeMillis(); 
System.out.println( "Operation" +s.getClass().getName()+ "Type is used for:" +(over-begin)+ "Ms" ); 
} 
public void test(StringBuffer s){ 
long begin = System.currentTimeMillis(); 
for(int i=0; i<time; i++){ 
s.append( " add " ); 
} 
long over = System.currentTimeMillis(); 
System.out.println( "Operation" +s.getClass().getCanonicalName()+ "Type is used for:" +(over-begin)+ "Ms" ); 
} 
public void test(StringBuilder s){ 
long begin = System.currentTimeMillis(); 
for(int i=0; i<time; i++){ 
s.append( " add " ); 
} 
long over = System.currentTimeMillis(); 
System.out.println( "Operation" +s.getClass().getName()+ "Type is used for:" +(over-begin)+ "Ms" ); 
} 

 
public void test2(){ 
String s2 =  " abcd " ; 
long begin = System.currentTimeMillis(); 
for(int i=0; i<time; i++){ 
String s = s2 + s2 +s2; 
} 
long over = System.currentTimeMillis(); 
System.out.println( "The time used for the operation string object reference addition type is:" +(over-begin)+ "Ms" ); 
} 
public void test3(){ 
long begin = System.currentTimeMillis(); 
for(int i=0; i<time; i++){ 
String s = " abcd "  +  " abcd "  +  " abcd " ; 
} 
long over = System.currentTimeMillis(); 
System.out.println( "The time used for operation string addition is:" +(over-begin)+ "Ms" ); 
} 
public static void main(String[] args){ 
String s1 =  " abcd " ; 
StringBuffer st1 = new StringBuffer( " abcd " ); 
StringBuilder st2 = new StringBuilder( " abcd " ); 
TestCharacter tc = new TestCharacter(); 
tc.test(s1); 
tc.test(st1); 
tc.test(st2); 
tc.test2(); 
tc.test3(); 
} 
} 

I ran this code under both myeclipse and DOS, and the printing time was somewhat different. The result is as follows:
1) when myeclipse cycles for 10000 times:

< img Alt = "" border = 0 SRC =" / / img.jbzj.com/file_images/article/201301/201301210845232.png ">

2) when myeclipse cycles for 50,000 times:

< img Alt = "" border = 0 SRC =" / / img.jbzj.com/file_images/article/201301/201301210845233.png ">

3) when running under DOS:

< img Alt = "" border = 0 SRC =" / / img.jbzj.com/file_images/article/201301/201301210845234.png ">


Related articles: