by Dinesh Thakur Category: array

int capacity () : This method returns the current capacity of the string buffer i.e. number of characters it is able to store without allocating more memory. While discussing capacity () methods, we assume that the StringBuffer object str1 exists which is defined as follows,

StringBuffer str1 = new ("Hello java");

For example,

str1. capacity ()

it will return 26 which is the sum of length of the string literal referenced by str1 and 16.

public class StringCapacity

         public static void main(String[] args)
            StringBuffer sl = new StringBuffer();
            StringBuffer s2 = new StringBuffer(35);
            StringBuffer s3 = new StringBuffer("Hello Java");
            System.out.println("sl.capacity()  =  " +sl.capacity());
            System.out.println("s2.capacity() = "+s2.capacity());
            System.out.println("s3.capacity () =  " + s3.capacity());

StringBuffer Capacity In Java Examples

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles