Home  >  Article  >  Java  >  The differences and principles of String, StringBuffer and StringBuilder in Java

The differences and principles of String, StringBuffer and StringBuilder in Java

(*-*)浩
(*-*)浩forward
2019-10-21 16:40:353481browse

First let’s talk about String:

The differences and principles of String, StringBuffer and StringBuilder in Java

Once a String object is created, its value cannot be modified. If you want to modify it, it will be reset. Open up memory space to store the modified object, that is, modify the String reference.

Because the bottom layer of String uses an array to store values, the length of the array cannot be changed, which leads to the above problem.

‌If we need to frequently modify a certain string during the actual development process, using String will cause a waste of memory space. How should we solve this problem? ‌

The answer is to use StringBuffer to solve this problem.

Let’s talk about StringBuffer in detail:

‌StringBuffer is similar to String. The bottom layer also uses an array to store the value of the string, and the default length of the array is 16, that is, an empty StringBuffer object

object, the array length is 16. Instantiating a StringBuffer object creates a 16-character string buffer.

But when we call the parameterized constructor to create a StringBuffer object, the length of the array is no longer 16, but the length of the array is determined based on the value of the current object.

The length of the array The length is "the length of the current object's value 16".

So after a StringBuffer is created, there is 16 characters of space to modify its value. If the modified value range exceeds 16 characters, it will first check whether the capacity of the original char array of the

StringBuffer object can hold the new string. If it cannot, the char array will be expanded.

How does StringBuffer expand?

The logic of expansion is to create a new char array, double the existing capacity and add 2. If it is still not large enough, it will directly equal the required capacity. After the expansion

is completed, copy the contents of the original array to the new array, and finally point the pointer to the new char array.

Let’s look at StringBuffer’s brother - StringBuilder

StringBuilder and StringBuffer have the same parent class, AbstractStringBuilder, and the implemented interfaces are exactly the same, both implemented

java.io.Serializable, CharSequence two interfaces.

Then what’s the difference between them?

The biggest difference is that StringBuffer implements synchronization for almost all methods, the thread is relatively safe, and data synchronization can be guaranteed in a multi-threaded system;

StringBuilder does not implement synchronization. It is not thread-safe and StringBuilder cannot be used in multi-threaded systems.

Usage scenarios of StringBuffer and StringBuilder:

Use StringBuffer when thread safety needs to be considered. If thread safety does not need to be considered, it can be used in scenarios where efficiency is pursued. StringBuilder.

The above is the detailed content of The differences and principles of String, StringBuffer and StringBuilder in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete
Previous article:Java IO knowledge pointsNext article:Java IO knowledge points