Usage and differences of string, StringBuffer, and StringBuilder in Java

Source: Internet
Author: User

String

The string is constructed in N (said N==11), and the common example is twos:

//1String S1 = "Hello World";//2String s2 =NewString ("Hello World");//3Char[] A = {' H ', ' e ', ' l ', ' l ', ' O '}; String S3=NewString (A, 1, 3);//Start Length//4String s4 = "Hello World"; String S5=NewString (S4);//CloneString s6 = S4;//Reference

Common methods of the string class:

String s = "Hello World";//ToArrayChar[] A =S.tochararray ();//charAtCharb = S.charat (0);//Get lengthintLen =s.length ();//Compareintc = S.compareto ("Hello"); return character DifferenceBooleanf = s.equals ("Hello");BooleanG = s = = "Hello";//address-level comparisons//Move BlankString h =S.trim ();//Cut and addString d = s.concat ("I Love You");//Hello world i love youString J = s + "I love You"; String e= S.substring (1, 3);//Out el startIndex endIndex
// int (or other types), String int a = += string.valueof (a);
StringBuffer & StringBuilder

Because the string object is immutable, each operation generates a new string object, so if you want to modify the string, use StringBuffer and StringBuilder.

StringBuffer s =NewStringBuffer ("Hello World");//DeleteS.delete (0, 6);//World Start End//ReverseS.reverse ();//Dlrow//AppendS.append ("World");//Dlrow World//InsertS.insert (0, 100);//100dlrow World//ReplaceS.replace (0, 3, "Hello");//Hellodlrow World//SetcharatS.setcharat (0, ' d ');//Dellodlrow World//String <-> StringBufferString A =s.tostring (); StringBuffer b=NewStringBuffer (a);
Advanced comparison

The main difference between string and StringBuffer and StringBuilder is certainly not as simple as modifying it.

Now that you have the string class, why StringBuffer and StringBuilder?

A string is a constant of strings , but it is immutable, that is, once created, any modification to it creates a new string object. StringBuffer string variables are thread- safe , and the methods provided by the StringBuilder class are exactly the same. If you look at the source code of Java (that is, the Java installation directory's Src.zip file), you will find that it differs from the method of the StringBuilder class by adding "synchronized" in front of each method to ensure that it is thread-safe. StringBuilder a string variable , which is thread insecure. It is indicated in the Java API that this class was only started in JDK 5 and is a single-threaded equivalence class for StringBuffer. (Two other string and StringBuffer classes, all starting with JDK 1.0)

Thread safety is a problem that needs to be considered in multi-threaded situations. For example, a StringBuilder may be accessed by multiple threads, and if there is no synchronized, the first thread changes its value, and then a second thread changes its value. When you switch back to the first thread, you are reading the modified value of the second thread. If multi-threading is not considered, StringBuilder is faster than StringBuffer.

  

Usage and differences of string, StringBuffer, and StringBuilder in Java

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.