Difference between String & StringBuffer & StringBuilder, javastring
StringThe String class is a final class, so it cannot be inherited. This means that the String content referenced by String cannot be modified.String can be instantiated in two ways.: (1) direct value assignment (in this example, String str = "Hello"; that is, direct value assignment is instantiated) (2) use new to call the constructor to complete instantiation;String str = "Hello"; srt: Only one objectString str2 = new String ("abc"); St2: two objects exist.
Public class LianXi2 {public static void main (String [] args) {String str = "Hello"; // reference str to object "Hello" str = str + "World "; // reference str to point to the object str + "World" (str + "World" is another object) System. out. println ("str =" + str );
}}Note that:
The content of a String object cannot be modified, but it does not mean that its reference cannot be changed.The following uses a memory distribution chart to describe the true meaning of a string that cannot be changed: three objects are opened in the heap memory: "Hello" and "world! "And" hello world! "These three objects. Str first points to the object "Hello". After splicing, str points to the object "hello world! ". The original content has not changed. Therefore, in future development, if you want to modify the String content frequently, try to use as few strings as possible, because the String pointing to "disconnected-connection" will greatly reduce the performance; we recommend that you modify the content frequently.
: StringBuilder and StringBuffer.
Stringbuffer: thread security
Stringbuilder: The thread is unsafe (with the highest performance)
There is no difference between the two classes.
String & Stringbuffer & Stringbuilder performance test
Package cn.com. string;/*** StringBuliderTest ** @ author: hushaoyu 5:49:55 * @ description: stringBulider performance demonstration */public class StringBuliderTest {// defines two constants: private static final String base = "abc"; private static final int count = 2000000; /*** test String Performance Method */public static void stringTest () {long begin, end; begin = System. currentTimeMillis (); // start time String test = new String (base); for (int I = 0; I <count/100; I ++) {test = test + "add";} end = System. currentTimeMillis (); // End Time System. out. println ("time used to test string:" + (end-begin);}/*** test stringbuffer Performance Method */public static void stringbufferTest () {long begin, end; begin = System. currentTimeMillis (); // start time StringBuffer test = new StringBuffer (base); for (int I = 0; I <count; I ++) {test = test. append ("add");} end = System. currentTimeMillis (); // End Time System. out. println ("time used to test StringBuffer:" + (end-begin);}/*** test stringbuilder Performance Method */public static void stringbuilderTest () {long begin, end; begin = System. currentTimeMillis (); // start time StringBuilder test = new StringBuilder (base); for (int I = 0; I <count; I ++) {test = test. append ("add");} end = System. currentTimeMillis (); // End Time System. out. println ("time used to test stringbuilder:" + (end-begin ));} /*** mai method ** @ param args */public static void main (String [] args) {stringTest (); stringbufferTest (); stringbuilderTest ();} /** output result: time used to test string: 1114 time used to test StringBuffer: 140 time used to test stringbuilder: 63 */}