Beginners Learn Java (iii)----StringBuilder class

Source: Internet
Author: User

Previous post Beginner Java (ii)----The difference between a string class and a StringBuffer class It is understood that the value of string is immutable, which results in

every The operation of String will generate a new string object, not only inefficient, but also a lot of waste of limited memory space, StringBuffer is variable

class, and A thread-safe string manipulation class that does not produce new objects for any of the strings it points to.

StringBuffer class and StringBuilder class functions 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 synchronizedby 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 thread A calls the Synchronized method m in the Class object o, it must obtain the lock of the object o 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:   

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");  }/* Tests 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 ("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 (); } }

The following is the result of 50,000 cycles, 10,000 times, 1000 times, 100 times of Operation:

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). Multi-threaded operation string buffers operation of large amounts of data = StringBuffer

Learn Java (iii)----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.