Java.lang.StringBuffer.capacity() Method

Advertisements


Description

The java.lang.StringBuffer.capacity() method returns the current capacity. The capacity is the amount of storage available for newly inserted characters, beyond which an allocation will occur.

Declaration

Following is the declaration for java.lang.StringBuffer.capacity() method

public int capacity()

Parameters

  • NA

Return Value

This method returns the current capacity.

Exception

  • NA

Example

The following example shows the usage of java.lang.StringBuffer.capacity() method.

package com.tutorialspoint;

import java.lang.*;

public class StringBufferDemo {

  public static void main(String[] args) {
  
    StringBuffer buff = new StringBuffer("TutorialsPoint");

    // returns the current capacity of the String buffer i.e. 16 + 14
    System.out.println("capacity = " + buff.capacity());
    
    buff = new StringBuffer(" ");
    // returns the current capacity of the String buffer i.e. 16 + 1
    System.out.println("capacity = " + buff.capacity());
  }
}

Let us compile and run the above program, this will produce the following result:

capacity = 30
capacity = 17



Advertisements
Advertisements