获取 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

更新于:2020 年 6 月 26 日

121 次浏览

开启你的 职业生涯

完成课程,获得认证

开始学习
广告
© . All rights reserved.