Fully explain the relationship between StringBuilder, StringBuffer, and string classes in Java _java

Source: Internet
Author: User
Tags stringbuffer
1. String class

The value of string is immutable, which causes a new string object to be generated every time a string is created, not only inefficient, but also a waste of limited memory space.
String a = "a"; Suppose a points to address 0x0001
A = "B";//After 0x0002, a points to address, but the "a" saved in the 0x0001 address still exists, but is no longer pointed to by a, and a has already pointed to another address.
Therefore, the operation of string is to change the assignment address instead of changing the value operation.

2. StringBuffer is a variable class, and thread-safe string manipulation class, and any action on the string it points to does not produce a new object. Each StringBuffer object has a buffer capacity that does not allocate new capacity when the string size does not exceed capacity, and automatically increases capacity when the string size exceeds capacity.

StringBuffer buf=new StringBuffer (); Allocate a character buffer of 16 bytes long
StringBuffer buf=new StringBuffer (512); Allocate a character buffer of 512 bytes long
StringBuffer buf=new StringBuffer ("This is a test")//holds a string in the buffer and reserves a 16-byte empty buffer at the back.

3.StringBuffer
The StringBuffer and StringBuilder functions are basically similar in that the main difference is that the StringBuffer class approach is multithreaded and secure, while StringBuilder is not thread-safe, compared to The StringBuilder class will be slightly faster. For strings that often change values, you should use the StringBuffer and StringBuilder classes.

4. Thread Safety
StringBuffer Thread Safety
StringBuilder thread is not secure

5. Speed
In general, speed from fast to slow: stringbuilder>stringbuffer>string, this comparison is relative, not absolute.

6. Summary
(1). If you want to manipulate a small amount of data with = String
(2). Manipulating large amounts of data under a single threaded operation string buffer = StringBuilder
(3). Manipulate a large amount of data under a multithreaded operation string buffer = StringBuffer

here is the Code and demo description:

Copy Code code as follows:

public class Testcharacter {
Final static int time = 50000; Number of 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 of time used is:" + (Over-begin) + "milliseconds");
}
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 of time used is:" + (Over-begin) + "milliseconds");
}
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 of time used is:" + (Over-begin) + "milliseconds");
}

/* Test string concatenation of strings directly * *
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 by the action string object reference additive type is: "+ (Over-begin) +" millisecond ");
}
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 adding the action string is: "+ (Over-begin) +" milliseconds ");
}
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 in MyEclipse and DOS, and each time it was printed out differently, the results were as follows:
1) under the MyEclipse Cycle 10,000 times:

2) under the MyEclipse cycle 50,000 times:

3 when running in DOS:

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.