Java StringBuffer capacity() Example

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

Java StringBuffer capacity() Example

Example: This simple program demonstrates the usage of capacity() method.
public class CapacityExample {
 public static void main(String[] args) {
  StringBuffer builder = new StringBuffer("javaguides");
  int capacity = builder.capacity();
  // inital capacity
  System.out.println(new StringBuffer().capacity());
  // intial capacity 16 + number of characters in string
  System.out.println("Capacity of the string :: " + capacity);
Capacity of the string :: 26

Free Spring Boot Tutorial - 5 Hours Full Course

Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course