The string class and the StringBuffer class in Java

Source: Internet
Author: User

There are 2 main differences between string and StringBuffer:

(1) The String class object is an immutable object, once you modify the value of the string object, implicitly re-create a new object, release the original string object, the StringBuffer class object is a modifiable object, you can modify the value by the Append () method

(2) The performance of a string class object is far less than the StringBuffer class.

The specific explanations are as follows:

There are 3 classes in Java that are responsible for the manipulation of characters.

1.Character is a single character operation,

2.String operates on a string of characters. Immutable classes.

3.StringBuffer is also an operation on a string of characters, but a mutable class.

String: The object is not of the original type. As an immutable object, you cannot modify its value once it has been created. The modification of a string object that already exists is to recreate a new object and then save the new value in. A String is the final class, which cannot be inherited.

StringBuffer: is a mutable object that, when modified, does not re-establish the object as a string, it can only be established by a constructor, StringBuffer sb = new StringBuffer (); Note: You cannot assign a value to a value by an assignment symbol. SB = "Welcome to here!"; After the/error object is established, the memory space is allocated in memory and a null is initially saved. When assigning a value to a stringbuffer, it is possible to pass its Append method. Sb.append ("Hello");

StringBuffer is more efficient in string join operations than string:

String str = new String ("Welcome to"); str + = "Here"; The processing step is actually through the establishment of a stringbuffer, let Hou call append (), and finally will StringBuffer tosting (); In this case, the connection operation of string is more than stringbuffer some additional operations, of course, the efficiency of the compromise.

And because the string object is an immutable object, each operation sting a new object to hold the new value. So the original object is useless, it will be garbage collection. This is also to affect performance.

The string class and the StringBuffer class in Java

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.