java.lang.StringBuffer capacity()

Description :

This java tutorial shows how to use the capacity() method of StringBuffer class under java.lang package. The capacity method of StringBuffer class gives the allowable number of characters this StringBuffer object can still accommodate.

Method Syntax :

public StringBuffer capacity()

Parameter Input :

DataType Parameter Description

Method Returns :

This method returns the current capacity. The capacity is the amount of storage available for newly inserted characters, beyond which an allocation will occur.

Compatibility Version :

Requires Java 1.0 and up

Java Code Example :

This java example source code demonstrates the use of capacity() method of StringBuffer class. Initially the code displays the capacity of the StringBuffer without any characters on the buffer. And then this code takes a user input and then the program will display the new capacity. This source code is also good example in learning on how to initialize StringBuffer object, modify the contents of the StringBuffer object.

Sample Output :

Running the capacity() method example source code of StringBuffer class will give you the following output

Exception Scenario :

Similar Method :

  • N/A

Suggested Reading List :

References :