A Java question every day [5]
Question
What are the similarities and differences between String, StringBuilder, and StringBuffer?
Answer
Similarities: String, StringBuilder, and StringBuffer can be used to store strings.
Differences:
1. The main difference between String, StringBuilder, and StringBuffer is that after a String object is created, it cannot be changed. We usually assign values to the same String variable. In fact, we create a new object. The latter two are mutable, which means they can change their values on the same memory address without creating new objects.
2. The difference between StringBuilder and StringBuffer is that StringBuffer is linear and secure, but it is slower than StringBuilder.
Reference Code
StringBufferThreadSafeTest
Package me. huangzijian; public class StringBufferThreadSafeTest {public static void main (String [] args) {// test StringBuffer thread security part String s = "123456789"; StringBuffer stringBuffer = new StringBuffer (s ); stringBuilder stringBuilder = new StringBuilder (s); SbfReverseThread sbfRT1 = new SbfReverseThread (stringBuffer); SbfReverseThread sbfRT2 = new shard (stringBuffer); fig = new shard (stringBuilder ); sbdReverseThread sbdRT2 = new SbdReverseThread (stringBuilder); new Thread (sbfRT1 ). start (); new Thread (sbfRT2 ). start (); new Thread (sbdRT1 ). start (); new Thread (sbdRT2 ). start (); // test the slow part of StringBuffer than StringBuilder. Be sure not to run two cycles at the same time and run one at a time so that long startTime2 = System can be precise. currentTimeMillis (); for (int I = 0; I <= 10000000; I ++) {stringBuffer. reverse ();} long endTime2 = System. currentTimeMillis (); System. out. println ("time required for StringBuffer loop 1000 times:" + (endTime2-startTime2) + "ms"); long startTime1 = System. currentTimeMillis (); for (int I = 0; I <= 10000000; I ++) {stringBuilder. reverse ();} long endTime1 = System. currentTimeMillis (); System. out. println ("StringBuilder cycle time required for 1000 times:" + (endTime1-startTime1) + "ms ");}}
SbfReverseThread
package me.huangzijian;public class SbfReverseThread implements Runnable { StringBuffer stringBuffer; public SbfReverseThread(StringBuffer stringBuffer) { this.stringBuffer = stringBuffer; } @Override public void run() { for (int i = 0; i <= 1000; i++) { try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } stringBuffer.reverse(); System.out.println("StringBuffer:" + stringBuffer); } }}
SbdReverseThread
package me.huangzijian;public class SbdReverseThread implements Runnable { StringBuilder stringBuilder; public SbdReverseThread(StringBuilder stringBuilder) { this.stringBuilder = stringBuilder; } @Override public void run() { for (int i = 0; i <= 1000; i++) { try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } stringBuilder.reverse(); System.out.println("StringBuilder:" + stringBuilder); } }}
The result shows that:
1. For StringBuilder, when two threads run for string 123456789 for a period of time, confusion begins, no longer 123456789 or 987654321, and StringBuffer is still 123456789 or 987654321 no matter how many times it is repeated.
2. on my machine, the StringBuffer string reversely runs 10000000, which takes 10000000 ms, while the StringBuilder string reversely runs, which only takes about Ms. It can be seen that StringBuilder is faster than StringBuffer.