Java Development StringBuilder Class

Source: Internet
Author: User

The StringBuffer class and the StringBuilder class function are basically similar. It's a two twins.
The following is mainly said two points

1th Thread Safety
StringBuffer Thread Safety
StringBuilder thread is not secure

Knowledge of thread safety is learning, just contact, not too deep understanding, in this knowledge a little mention.

Thread safety-If your code is in a process where multiple threads are running concurrently, these threads may run the code at the same time. If every
The result of the run is the same as the single-threaded run, and the values of the other variables are the same as expected, which is thread-safe.

There is no big difference between the StringBuffer class and the StringBuilder class, but in terms of thread safety, StringBuffer allows multithreading for word
operator. This is because many of the methods StringBuffer in the source code are synchronized by the keyword (this keyword is set for the thread synchronization mechanism
Of Modified, and StringBuilder not.

Simply say the meaning of synchronized:
Each class object corresponds to a lock, and when a Java technology thread A calls the Synchronized method m in the Class object O, the lock on the object o must be obtained to execute
M method, otherwise thread A is blocked. Once thread a starts executing the M method, the lock of the object o will be exclusive. Makes other lines that require the M method of the O object to be called satistical
Plug Only thread a executes, after the lock is released. Those blocking threads have a chance to call the M method again. This is the locking mechanism that solves the thread synchronization problem.

Therefore, StringBuffer is more secure than StringBuilder in multithreaded programming.

One thing to note is that some people ask, is the string class not safe? In fact, this problem does not exist, and the string is immutable. Line
A string object specified in the heap can only be read and cannot be modified. What's not safe with you?

2nd efficiency Issue
In general, speed from fast to slow: stringbuilder>stringbuffer>string, this comparison is relative, not absolute.
To give a simple example:
[Java] View plaincopy on code to see a snippet derived from my Code slice
public class testcharacter{
final static int time=100; 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 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");
}

/* Test string concatenation directly on strings */
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 ("Action string object reference additive type is used for:" + (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 operation string is added using the time:" + (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 ();
}

}

Java Development StringBuilder class

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.