- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Set the capacity value for a StringBuffer object in Java
The capacity() method returns the current capacity. It is a part of the StringBuffer. It is the quantity of storage present for recently inserted characters, after which an allocation occurs.
Declaration - The capacity() method is declared in the java.lang.StringBuffer class as follows −
public int capacity()
The capacity of a StringBuffer object can be set by inserting the value to be set while instantiating the StringBuffer class.
Let us see an example program showing how the capacity value can be set.
Example
import java.lang.*; public class Example { public static void main(String[] args) { StringBuffer b = new StringBuffer(100); System.out.println("Capacity for the StringBuffer Object = " + b.capacity()); b = new StringBuffer(" "); // returns the current capacity of the String buffer which is 16 + 1 System.out.println("Capacity for the StringBuffer Object = " + b.capacity()); } }
Output
Capacity for the StringBuffer Object = 100 Capacity for the StringBuffer Object = 17
- Related Articles
- Get the capacity of the StringBuffer Object in Java
- Change the default capacity of the StringBuffer Object in Java
- Get the length of a StringBuffer Object in Java
- Change a single character in a Java StringBuffer object in Java
- Remove a character from a Java StringBuffer object
- Change the length of the StringBuffer Object in Java
- Reverse the sequence of characters in a StringBuffer Object in Java
- What is the difference between a String object and a StringBuffer object in java?
- Remove characters in a range from a Java StringBuffer Object
- Call append() method to construct a StringBuffer object in Java
- How to set minimum capacity for arraylist Listview in Android?
- Create a StringBuffer object using a reference stored in a variable of type String in Java
- Java StringBuffer class.
- Is StringBuffer final in Java?
- Set a Pair value in Java Tuple

Advertisements