String immutable StringBuffer variable

Source: Internet
Author: User

String is a class or can represent a string data type

String: Is an object that is not the original type. As an immutable object, you cannot modify its value once it is 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. When passed as a parameter, just copy a reference

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 ();

After the object is created, 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");

The StringBuffer process is actually done by establishing a stringbuffer, then calling append (), and then StringBuffer tosting (); Insert method

Comparison

StringBuffer efficiency in string join operations is higher than string

String new object, garbage collection affects performance

Immutable Benefits:

1. String pooling is possible only if the string is immutable. The implementation of a string pool can save a lot of heap space at run time, because different string variables point to the same string in the pool. But if the string is mutable, then string interning will not implement the

2. The user name and password of the database are passed in as a string to obtain a connection to the database, or in socket programming, the hostname and port are passed in as a string. Because the string is immutable, its value is immutable, otherwise hackers can drill into the empty, change the value of the object that the string points to, resulting in a security breach.

3. Because strings are immutable, multithreading is shared securely, and the same string instance can be shared by multiple threads.

4. Because the string is immutable, hashcode is cached when it is created and does not need to be recalculated. This makes the string well suited as a key in the map, and the string is processed faster than other key objects. This is where keys in hashmap tend to use strings

5. The ClassLoader uses a string, and immutability provides security so that the correct class is loaded. For example, if you want to load the Java.sql.Connection class, and this value is changed to myhacked.connection, it will cause an unknown damage to your database.

Cons: Impact efficiency, impact on system performance. Especially when there are no more reference objects in memory, the JVM's GC will start to work, and the speed will be quite slow.

StringBuffer and StringBuilder variable

Difference

Changing the StringBuffer each time the result is performed on the StringBuffer object itself, instead of generating a new object and changing the object reference.

The StringBuilder class is designed to be used as a simple replacement for stringbuffer, which is common when a string buffer is used by a single thread. However, synchronization is not guaranteed. If possible, it is recommended that this class be preferred because, in most implementations, it is faster than StringBuffer. The two methods are basically the same.

String immutable StringBuffer variable

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.