Home  >  Article  >  Java  >  You have to figure out String, StringBuilder, StringBuffer

You have to figure out String, StringBuilder, StringBuffer

无忌哥哥
无忌哥哥Original
2018-07-20 09:37:071273browse

The relationship and difference between the three classes of String, StringBuilder and StringBuffer has always been a classic issue in Java. This time I will talk about some knowledge about these three classes

1. Simple comparison

  • String: Character constant

  • StringBuilder: Character variable

  • StringBuffer: Character variable

String is a constant type and is declared as final class. All properties are also final type, so once the String object is created, it cannot be changed;
StringBuilder / StringBuffer two classes belong to variable types, can be changed, they are all classes provided to solve the problem of too many intermediate objects generated by string concatenation.

  • Running speed StringBuilder > StringBuffer > String

  • Thread safety: StringBuffer

  • non Thread safety: StringBuilder

StringBuilder is not much different from StringBuffer in essence, but because StringBuilder removes the thread safety part owned by StringBuffer, it effectively reduces the overhead. Therefore, StringBuilder is the first choice for string splicing operations in most cases

2. String processing strings

Example 1:

String s = "abcd";
s = s + "fgh";

Many people When doing such string processing, it will be mistakenly believed that the String type is variable.

But in fact, the process of JVM processing this code is as follows: first create an s object, assign the value "abcd", and then when processing the second line of code, create another s object, assign the value "abcdfgh", and then The first s object is garbage collected.

So it is equivalent to the first s being unchanged and the second s being a new object

Example 2:

String str = “This is only a” + “simple” + “test”;

This code is equivalent to String str = “This is only a simple test”;

Example 3:

String str2 = "This is only a";
String str3 = "simple";
String str4 = "test";
String str1 = str2 +str3 + str4;

This code will also be processed according to the process of Example 1

Three. StringBuilder / StringBuffer construction characteristics

During the construction process of these two objects, first apply for a character array (char[]) according to the default size. The default capacity is 16 characters, but if it exceeds, Arrays will be used. copyOf() doubles the capacity to 16, 32, 64, 128..., of course this will affect performance, so you can customize its capacity as needed when creating the object

//默认 16 个字符
public StringBuilder() {
    super(16);
}
//构造函数定义容量
public StringBuilder(int capacity) {
    super(capacity);
}

4. String and StringBuilder process strings Splicing comparison

We all know that StringBuilder is recommended for string splicing operations, but is it always recommended to use StringBuilder instead of String for string splicing? Obviously not.

Example 1:

String str = "123";
String str1 = str + "456";
String str2 = new StringBuilder().append(str).append("def").toString();

In this case, there is little difference in efficiency between the two processing methods

InJDK 8, the string splicing operation of String will be automatically converted to StringBuilder by the compiler and the append method will be called. Due to this optimization scheme, the processing efficiency of the two classes in this case is not much different; while in JDK In 9, in order to more uniformly optimize string operations, StringConcatFactory is provided as a unified entrance to further optimize string splicing operations.

Example 2:

String str = "";
for (int i = 0; i < 1000; i++) {
    str += "12345";
}

StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < 1000; i++) {
    stringBuilder.append("12345");
}

In this case, StringBuilder is faster

In the loop, every time " " is executed, a String will be created Objects, so there will be a lot of consumption of object creation and recycling.

Simply put, in a loop for string splicing of the same string object, StringBuilder is preferred

Example 3

String str1 = "123" + "456" + "789";

String str2 = new StringBuilder("123").append("456").append("789").toString();

In this case, String is faster

We all know thatString str1 = "123" "456" "789"; is actually equivalent to String str1 = "123456789";, but StringBuilder needs to call the append method multiple times.

The above is the detailed content of You have to figure out String, StringBuilder, StringBuffer. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn