Two---Strings of the Java Learning Series (char. String.stringbuilder and StringBuffer)

Source: Internet
Author: User

one. String

1.String: string constant, the length of the string is immutable. String in Java is immutable (immutable). The string class is final decorated

2.String str= The difference between "Hello World" and string Str=new string ("Hello World")

publicclassMain {

              public static void main(String[] args) {          String str1 =  "hello world" ;          String str2 =  new String( "hello world" );          String str3 =  "hello world" ;          String str4 =  new String( "hello world" );                   System.out.println(str1==str2);          System.out.println(str1==str3);          System.out.println(str2==str4);      } } two.StringBuffer (JDK1.0)

StringBuffer: String variable (Synchronized, thread safe). If you want to modify the string content frequently, it is best to use stringbuffer for efficiency reasons, and if you want to turn to string type, you can call the ToString () method of StringBuffer.

Java.lang.StringBuffer A variable sequence of characters for thread safety. It contains a specific sequence of characters at any point in time, but some method calls can change the length and content of the sequence. String buffers can be safely used with multiple threads.

The main operations on StringBuffer are the Append and insert methods, which can be overloaded to accept arbitrary types of data. Each method effectively converts the given data into a string, and then appends or inserts the character of the string into the string buffer. The Append method always adds these characters to the end of the buffer, while the Insert method adds characters at the specified point. For example, if z refers to a string buffer object where the current content is "start", this method call Z.append ("le") causes the string buffer to contain "startle", and Z.insert (4, "le") will change the string buffer to contain " Starlet ".

3 StringBuilder (JDK5.0)

StringBuilder: String variable (non-thread safe). Internally, the StringBuilder object is treated as a variable-length array that contains a sequence of characters.

Java.lang.StringBuilder is a variable sequence of characters that is JDK5.0 new. This class provides an API that is compatible with StringBuffer, but does not guarantee synchronization. This class is designed to be used as a simple replacement for stringbuffer, and is used when a string buffer is used by a single thread (this is a common situation)

5 Usage Policies

(1) Basic principles: If you want to manipulate a small amount of data, using string, single-threaded operation of large amounts of data, with StringBuilder, multi-threaded operation of a large number of data, with StringBuffer.

(2) Do not use the String class "+" for frequent splicing , because that performance is very poor, should use the StringBuffer or StringBuilder class, which is a more important principle in Java optimization.

Http://www.cnblogs.com/dolphin0520/p/3778589.html

Two---Strings of the Java Learning Series (char. String.stringbuilder and StringBuffer)

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.