Java StringBuffer int Capacity()方法与示例

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值