StringBuffer类int Capacity() (StringBuffer Class int capacity())
This method is available in package java.lang.StringBuffer.capacity().
软件包java.lang.StringBuffer.capacity()中提供了此方法。
This method is used to return the capacity of the StringBuffer(i.e. It is the amount of extra storage for the object that we can store beyond allocation).
此方法用于返回StringBuffer的容量(即,这是我们可以分配的超出对象的额外存储量)。
The formula to find the capacity of any StringBuffer object is 16 + "No. of characters".
查找任何StringBuffer对象的容量的公式为16 +“字符数” 。
Syntax:
句法:
int capacity(){
}
Parameter(s):
参数:
We don't pass any object as a parameter in the method of the StringBuffer.
在StringBuffer的方法中,我们不传递任何对象作为参数。
Return value:
返回值:
The return type of this method is int that means this method returns capacity of the StringBuffer object in number.
此方法的返回类型为int ,这意味着该方法以数字形式返回StringBuffer对象的容量。
Java程序演示Capacity()方法的示例 (Java program to demonstrate example of capacity() method)
import java.lang.StringBuffer;
public class StringBufferClass {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Java is a programming language");
// use capacity() it will return the capacity(i.e 16 + 30)
// of the StringBuffer object sb.
System.out.println("The capacity of the StringBuffer object is :" + sb.capacity());
sb = new StringBuffer("10");
// use capacity() it will return the capacity(i.e. 16 + 2)
// of the another StringBuffer object sb .
System.out.println("The capacity of the another StringBuffer object is :" + sb.capacity());
sb = new StringBuffer(" ");
// use capacity() it will return the capacity(i.e. 16 + 1)
// of the another StringBuffer object sb.
System.out.println("The capacity of the another StringBuffer object is :" + sb.capacity());
}
}
Output
输出量
D:\Programs>javac StringBufferClass.java
D:\Programs>java StringBufferClass
The capacity of the StringBuffer object is :46
The capacity of the another StringBuffer object is :18
The capacity of the another StringBuffer object is :17
翻译自: https://www.includehelp.com/java/stringbuffer-int-capacity-method-with-example.aspx