import java.io.*;
class GFG {
public static void main(String[] args) {
StringBuffer s = new StringBuffer("AAA");
int p = s.length();
int q = s.capacity();
System.out.println("Length of string =" + p);
System.out.println("Capacity of string =" + q);
}
}
it gives output:
Length of string =3 Capacity of string =19
Actually the length can be found by the number of characters in string,So I know the direct answer without IDE.But what is exactly way to calculate capacity of a string.How can I get an idea of a string capacity without using any IDE.
The capacity is the size of the internal buffer. You usually only care about when you pre-size the buffer for constructing a string you already know the maximum size of. String buffers/builders automatically increase their capacity when they need to.