First, the difference between the string class and the StringBuffer class, the string class is a constant, cannot be added, and StringBuffer is a character buffer, you can add a string inside. For example:
<span style= "FONT-SIZE:18PX;" >string str = "HelloWorld"; str + = "Welcome";</span>
The process here actually is this: generated a String object "HelloWorld" reference held by STR when executing str + = "Welcome"; This line of code is actually discarded the original "HelloWorld" object and then produced a new splicing object, and then the reference to the Str. Therefore, a new string object is generated in the process, and the original object is reclaimed by the garbage collector. must be to affect the performance of the program.
So, let's take a look at StringBuffer's approach now.
<span style= "FONT-SIZE:18PX;" >stringbuffer SB = new StringBuffer ("HelloWorld"); Sb.append ("Welcome");</span>
The process of stringbuffer is like this. First, a StringBuffer object is constructed, in which the specified string sequence can be added directly to the buffer.
There is one other way of writing that must be wrong:
StringBuffer sb = new StringBuffer ();
SB = "HelloWorld"; How can a string object be assigned to a StringBuffer object?
Then the following test code snippet for the performance test of the string class and the StringBuffer class
The execution effect is not absolute, depending on the hardware configuration of each machine.
The first is the performance of the String class
<span style= "FONT-SIZE:18PX;" >/* This code snippet tests the performance of the string class by about 2854 milliseconds string tempvalue = "HelloWorld"; int times = 10000; String value = ""; Long StartTime = System.currenttimemillis (); for (int i = 0; I < times; i++) {value + = Tempvalue;} Long endTime = System.currenttimemillis (); System.out.println (endtime-starttime);*/</span>
And then the StringBuffer class.
<span style= "FONT-SIZE:18PX;" >/* This code snippet is used to test the performance of the StringBuffer class for approximately 10 milliseconds string tempvalue = "HelloWorld"; int times = 10000; StringBuffer sb = new StringBuffer (); Long startTime = System.currenttimemillis (); for (int i = 0; I < times; i++) {Sb.ap Pend (Tempvalue);} Long endTime = System.currenttimemillis (); System.out.println (endtime-starttime);*/</span>
In one is the comparison between the StringBuilder and the StringBuffer class
How to say the StringBuilder class is a class that appears starting with JDK5.0. Used to replace StringBuffer. However, thread safety is not guaranteed. If you need this kind of synchronization, use the StringBuffer class, and the StringBuilder class is thread insecure. And StringBuffer is thread-safe.
In terms of performance, StringBuilder is a little bit better than StringBuffer's performance, but the two classes generally use the same method.
Test code snippet that tests the performance of the StringBuilder class.
<span style= "FONT-SIZE:18PX;" >/* This code snippet is used to test the performance of the StringBuilder class by about 10 milliseconds string tempvalue = "HelloWorld"; int times = 10000; StringBuilder sb = new StringBuilder (); long startTime = System.currenttimemillis (); for (int i = 0; I < times; i++) {sb.append (tempvalue);} Long endTime = System.currenttimemillis (); System.out.println (endtime-starttime);*/</span>
In summary: If you are using a constant for more, please also use the String class, if you change the value frequently, also use StringBuffer or StringBuilder.
Generally performance on StringBuilder > StringBuffer > String
Differences between the String class StringBuffer class and the StringBuilder class in Java