獲取 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.