StringBuilder is no longer needed to concatenate strings in Java 8
Among Java developers, the high resource consumption of string splicing is often a hot topic.
Let’s discuss in depth why it takes up so many resources.
In Java, a string object is immutable, meaning that once it is created, you cannot change it. So when we concatenate strings, a new string is created, and the old one is marked by the garbage collector.
If we process millions of strings, then we will generate millions of additional strings to be processed by the garbage collector.
The bottom layer of the virtual machine performs many operations when splicing strings. The most direct dot operator for concatenating strings is the String#concat(String) operation.
public String concat(String str) { int otherLen = str.length(); if (otherLen == 0) { return this; } int len = value.length; char buf[] = Arrays.copyOf(value, len + otherLen); str.getChars(buf, len); return new String(buf, true); }
public static char[] copyOf(char[] original, int newLength) { char[] copy = new char[newLength]; System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); return copy; }
void getChars(char dst[], int dstBegin) { System.arraycopy(value, 0, dst, dstBegin, value.length); }
You can see that a character array is created, and the length is the sum of the length of the existing characters and the concatenated characters. Their values are then copied into a new character array. Finally, create a String object from this character array and return it.
So these operations are numerous. If you calculate it, you will find that the complexity is O(n^2).
To solve this problem, we use the StringBuilder class. It's like mutable String class. The splicing method helps us avoid unnecessary duplication. It has a complexity of O(n), which is far better than O(n^2).
However, Java 8 uses StringBuilder to concatenate strings by default.
Java 8 documentation:
In order to improve the performance of string concatenation, the Java compiler can use the StringBuffer class or similar technology to reduce the creation of intermediate String objects when using evaluation expressions.
The Java compiler handles this situation:
public class StringConcatenateDemo { public static void main(String[] args) { String str = "Hello "; str += "world"; } }
The above code will be compiled into the following bytecode:
public class StringConcatenateDemo { public StringConcatenateDemo(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]); Code: 0: ldc #2 // String Hello 2: astore_1 3: new #3 // class java/lang/StringBuilder 6: dup 7: invokespecial #4 // Method java/lang/StringBuilder."<init>":()V 10: aload_1 11: invokevirtual #5 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 14: ldc #6 // String world 16: invokevirtual #5 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 19: invokevirtual #7 // Method java/lang/StringBuilder.toString:()Ljava/lang/String; 22: astore_1 23: return }
You can see in these bytecodes that StringBuilder is used. So we no longer need to use StringBuilder class in Java 8.
English original text: We Don't Need StringBuilder for Concatenation Anymore
The above is the detailed content of StringBuilder is no longer needed to concatenate strings in Java 8. 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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Start Spring using IntelliJIDEAUltimate version...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...
