Home >Java >javaTutorial >Use java's StringBuilder.capacity() function to get the capacity of the string buffer

Use java's StringBuilder.capacity() function to get the capacity of the string buffer

王林
王林Original
2023-07-24 11:15:22891browse

StringBuilder is a commonly used string manipulation class in Java. It provides a series of methods to operate on strings. In many scenarios, we need to splice strings, and often need to modify the length of the strings. In order to perform string operations efficiently, Java provides the StringBuilder class to replace the String class.

StringBuilder class internally uses a character array to store the content of the string. This character array has an initial capacity. When the length of the string exceeds the capacity, StringBuilder will automatically expand the capacity. In order to avoid frequent expansion operations, we can manually set the initial capacity to reduce memory usage and improve performance. The capacity() function of StringBuilder can obtain the capacity of the string buffer.

The following is a code example using the StringBuilder class:

public class StringBuilderExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        
        // 打印初始容量
        System.out.println("初始容量:" + sb.capacity());
        
        // 添加字符串
        sb.append("Hello");
        
        // 打印容量
        System.out.println("添加 Hello 后的容量:" + sb.capacity());
        
        // 继续追加字符串
        sb.append("World");
        
        // 打印容量
        System.out.println("添加 World 后的容量:" + sb.capacity());
        
        // 再次追加字符串
        sb.append("Java is awesome!");
        
        // 打印容量
        System.out.println("添加 Java is awesome! 后的容量:" + sb.capacity());
    }
}

In this example, we first create a StringBuilder object sb. In the initial state, the capacity of StringBuilder is 16. By calling the capacity() function, we can obtain the initial capacity.

Next, we use the append() function to append a string to the sb object. Each time we append, we can get the current capacity through the capacity() function. After appending "Hello", the capacity is 16. After adding "World", the capacity is 16. After appending "Java is awesome!", the capacity is 34. From this example we can see that StringBuilder will automatically expand when the string length exceeds the original capacity.

By manually setting the initial capacity, we can reduce the number of expansions, thereby improving the performance and efficiency of the code. In actual development, when we need to splice a large number of strings, we can first set the initial capacity based on the estimated total length of the strings. This can effectively avoid frequent expansion operations, thereby improving code execution efficiency.

In summary, by using the capacity() function of the StringBuilder class, we can get the capacity of the string buffer. Using this function, we can operate strings more efficiently when writing code, while reducing memory usage and improving code execution efficiency. In actual development, it is necessary to select the appropriate initial capacity according to specific business scenarios to obtain better performance.

The above is the detailed content of Use java's StringBuilder.capacity() function to get the capacity of the string buffer. 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