You have to figure out String, StringBuilder, StringBuffer
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Convert basic data types to strings using Java's String.valueOf() function In Java development, when we need to convert basic data types to strings, a common method is to use the valueOf() function of the String class. This function can accept parameters of basic data types and return the corresponding string representation. In this article, we will explore how to use the String.valueOf() function for basic data type conversions and provide some code examples to

Method of converting char array to string: It can be achieved by assignment. Use {char a[]=" abc d\0efg ";string s=a;} syntax to let the char array directly assign a value to string, and execute the code to complete the conversion.

The methods to clear stringbuilder are: 1. Use the setLength(0) method to clear the StringBuilder object; 2. Use the delete(0, length) method to clear the StringBuilder object; 3. Use the replace(0, length, "") method to clear the StringBuilder object; 4. , Use new StringBuilder() to re-create a new StringBuilder object.

Use the delete() method of the StringBuilder class in Java to delete part of the content in a string. The String class is a commonly used string processing class in Java. It has many commonly used methods for string operations. However, in some cases, we need to frequently modify strings, and the immutability of the String class will lead to frequent creation of new string objects, thus affecting performance. To solve this problem, Java provides the StringBuilder class, which

The append() method of StringBuilder class accepts a String value and adds it to the current object. Convert string value to StringBuilder object - Get string value. Append using the append() method to get the string into the StringBuilder. Example In the following Java program, we are converting an array of strings into a single StringBuilder object. Real-time demonstration publicclassStringToStringBuilder{ publicstaticvoidmain(Stringargs[]){&a

Replace characters (strings) in a string using Java's String.replace() function In Java, strings are immutable objects, which means that once a string object is created, its value cannot be modified. However, you may encounter situations where you need to replace certain characters or strings in a string. At this time, we can use the replace() method in Java's String class to implement string replacement. The replace() method of String class has two types:

Interpretation of Java documentation: Detailed introduction to the substring() method of the StringBuilder class Introduction: In Java programming, string processing is one of the most common operations. Java provides a series of classes and methods for string processing, among which the StringBuilder class is a commonly used choice for frequent string operations. In the StringBuilder class, the substring() method is a very useful method for intercepting substrings of strings. This article will

Hello everyone, today I will share with you the basic knowledge of Java: String. Needless to say the importance of the String class, it can be said to be the most used class in our back-end development, so it is necessary to talk about it.
