获取 Java 中 StringBuffer 对象的容量
capacity() 方法是 StringBuffer 类的其中一个方法。它会返回为新插入字符提供存储容量的数量,存储分配完成后会发生此过程。
声明 - java.lang.StringBuffer.capacity() 方法的声明如下:−
public int capacity()
我们来看一个示例程序,了解如何获取 StringBuffer 对象的容量。
示例
import java.lang.*;
public class Example {
public static void main(String[] args) {
StringBuffer b = new StringBuffer(“Hello”);
// returns the current capacity of the String buffer which is 16 + 5 = 21
System.out.println("Capacity for the StringBuffer Object = " + b.capacity());
}
}输出
Capacity for the StringBuffer Object = 21
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP