What is the difference between stringbuffer and stringbuilder in string?
Jul 02, 2020 pm 02:14 PMThe difference between stringbuffer and stringbuilder in string is: 1. StringBuffer is thread-safe, while StringBuilder is thread-unsafe; 2. StringBuffer exposes method synchronization, but StringBuilder does not.
The difference between stringbuffer and stringbuilder in string is:
Difference 1: Thread safety
StringBuffer: thread-safe, StringBuilder: thread-unsafe. Because all public methods of StringBuffer are synchronized, and StringBuilder is not StringBuilder modified.
StringBuffer code snippet:
@Override public synchronized StringBuffer append(String str) { toStringCache = null; super.append(str); return this; }
Difference 2: Buffer
StringBuffer code snippet:
private transient char[] toStringCache; @Override public synchronized String toString() { if (toStringCache == null) { toStringCache = Arrays.copyOfRange(value, 0, count); } return new String(toStringCache, true); }
StringBuilder code snippet:
@Override public String toString() { // Create a copy, don't share the array return new String(value, 0, count); }
It can be seen that every time StringBuffer obtains toString, it will directly use the toStringCache value of the buffer area to construct a string.
StringBuilder needs to copy the character array every time and then construct a string.
So, cache flushing is also an optimization of StringBuffer, but the toString method of StringBuffer is still synchronous.
Difference 3: Performance
Since StringBuffer is thread-safe, all its public methods are synchronized, and StringBuilder does not lock and synchronize methods, so There is no doubt that the performance of StringBuilder is much greater than StringBuffer.
Related learning recommendations: Java video tutorial
The above is the detailed content of What is the difference between stringbuffer and stringbuilder in string?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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

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

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

Use java's StringBuilder.replace() function to replace a specified range of characters. In Java, the StringBuilder class provides the replace() method, which can be used to replace a specified range of characters in a string. The syntax of this method is as follows: publicStringBuilderreplace(intstart,intend,Stringstr) The above method is used to replace the index star from

Use java's StringBuilder.insert() function to insert a string at a specified position. StringBuilder is a class in Java used to handle variable strings. It provides a variety of methods to operate strings. The insert() function is used to insert strings at specified positions. One of the common methods of positionally inserting strings. In this article, we will introduce how to use the insert() function to insert a string at a specified position and give corresponding code examples. insert()

How does Java use the substring() function of the StringBuilder class to intercept a substring of a string? In Java, we often need to process string operations. Java's StringBuilder class provides a series of methods to facilitate us to operate strings. Among them, the substring() function can be used to intercept substrings of strings. The substring() function has two overloaded forms, namely substring(intstar

Use java's StringBuilder.ensureCapacity() function to set the minimum capacity of the string buffer. StringBuilder is one of the common string manipulation classes provided in Java. It can add, delete, and delete strings based on the current string buffer. Modification and other operations are more efficient and flexible than the String class. However, when performing a large number of string concatenation operations, the performance of StringBuilder will also be affected to a certain extent.
