The difference between string, StringBuilder, and StringBuffer in Java

Source: Internet
Author: User
Tags stringbuffer

A. String class

string classes represent strings. All string literals in a Java program, such as "abc" ) are implemented as instances of this class.
A. String class
string classes represent strings. All string literals in a Java program, such as "ABC", are implemented as instances of this class.

Strings are constants, and their values cannot be changed after they are created. A string buffer supports a mutable string. Because the String object is immutable, it can be shared. For example:

String str = "ABC";

is equivalent to:

char[] data = {' A ', ' B ', ' C '};

String str = new string (data);

The Java language provides special support for string concatenation symbols ("+") and for converting other objects to strings. string concatenation is implemented through the StringBuilder (or StringBuffer) class and its Append method. String conversions are implemented through the ToString method, which is defined by the Object class and can be inherited by all classes in Java.

Therefore, str = "Here"; Equivalent to:

StringBuilder sb = new StringBuilder (str);

Sb.append ("here");

str = sb.tostring ();

When you add a string, you create a StringBuilder object, and then append, the last ToString (), which is much less efficient than the append () that directly uses StringBuilder (or StringBuffer). Therefore, when stitching strings, it is advocated to use StringBuilder (or stringbuffer) instead of strings being added directly.

Next, we'll discuss the construction method: public string (string original), which initializes a newly created string object to represent a sequence of characters that is the same as the argument; in other words, the newly created string is a copy of the parameter string. Because a String is immutable, you do not need to use this construction method unless you need an explicit copy of the original.

So for string str = new String ("here"); The compiler first checks in string pool for a string with the content "here", and if the string "Here" already exists in the sting pool, the program creates a string "Here" in the heap from the new string ("Here") if sting There is no "here" in the pool, and when executed, an object is created in string pool and in the heap, with the contents of the object "here", and Str pointing to "here" in the heap.

Obviously, we normally use string str = "Here" directly; Can be done without constructing the method.

Two. StringBuffer class

StringBuffer is a thread-safe variable character sequence. A string buffer similar to strings, but cannot be modified. Although it contains a particular sequence of characters at any point in time, some method calls can change the length and content of the sequence.

You can safely use string buffers with multiple threads. These methods can be synchronized as necessary, so all operations on any particular instance occur as if they were in a sequential order, in the same order as the method calls of each of the involved threads.

The main operations on StringBuffer are the Append and insert methods, which can be overloaded to accept any type of data. Each method effectively converts the given data to 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, and the Insert method adds the characters at the specified point.

For example, if z references a string buffer object with current content of "start", this method call Z.append ("le") causes the string buffer to contain "startle" and Z.insert (4, "le") changes the string buffer to include "Starle T ".

Typically, if SB references an instance of StringBuilder, Sb.append (x) and Sb.insert (Sb.length (), x) have the same effect.

When an operation related to a source sequence occurs, such as an append or insert operation in a source sequence, the class implements synchronization only on the string buffer that executes the operation, not on the source.

Each string buffer has a certain capacity. You do not need to allocate a new internal buffer array as long as the string buffer contains a sequence of characters that does not exceed this capacity. This capacity is automatically increased if the internal buffer overflows.

StringBuffer is a mutable object that, when modified, does not re-establish an object like string, and it can only be created through constructors:
StringBuffer sb = new StringBuffer ();
Note: You cannot assign a value to him by an assignment symbol.
SB = "Welcome to here!"; Error
When an object is created, memory space is allocated in memory and a null is initially saved. StringBuffer is much more efficient in string concatenation operations than string.

Three. StringBuilder class

The StringBuilder class was added starting with JDK 5, which provides a stringbuffer-compliant API, but does not guarantee synchronization. This class is designed as a simple replacement for stringbuffer, which is common when a string buffer is used by a single thread. It is generally preferable to use the StringBuilder class when compared to the StringBuffer class, because it supports all the same operations, but is faster because it does not perform synchronization. If possible, it is recommended that the class be preferred, because in most cases it is faster than StringBuffer.

Note: It is not safe to use an instance of StringBuilder for multiple threads. If such a synchronization is required, the StringBuffer class is recommended.

This article comes from: Development Institute http://edu.codepub.com original Link: http://edu.codepub.com/2010/1030/26838.php

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.