The difference between Java Foundation--string and StringBuffer

Source: Internet
Author: User

First, there are three classes in Java that manipulate strings:

    1. Character is a single character operation;
    2. String to manipulate a string of characters. immutable classes;
    3. StringBuffer also operates on a string of characters, but the variable class

String:
Is the object is not 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 does not re-establish the object as a string when modifying it
It can only be created by a constructor function,
StringBuffer sb = new StringBuffer ();
Note: You cannot pay a value by paying the value symbol.
SB = "Welcome to here!"; /error
After the object is created, the memory space is allocated in memory and a null is initially saved. to StringBuffer
The Append method can be used when paying the value.
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 by building a stringbuffer, let Hou call append (), and finally
Then the 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 difference between Java Foundation--string and StringBuffer

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.