Java StringBuffer capacity() Method

The StringBuffer.capacity() method in Java is used to return the current capacity of the StringBuffer object. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. capacity Method Syntax
  3. Examples
    • Initial Capacity
    • Capacity Growth
    • Specified Initial Capacity
  4. Conclusion

Introduction

The StringBuffer.capacity() method is a member of the StringBuffer class in Java. The capacity of a StringBuffer object represents the amount of storage available for newly appended characters, beyond which the buffer will need to be reallocated. Understanding and managing the capacity of a StringBuffer can help optimize memory usage and improve performance in scenarios that involve extensive string manipulation.

capacity Method Syntax

The syntax for the capacity method is as follows:

public synchronized int capacity()

This method returns the current capacity of the StringBuffer object.

Examples

Initial Capacity

When a StringBuffer is created without specifying an initial capacity, it uses a default capacity.

Example

public class StringBufferCapacityExample {
    public static void main(String[] args) {
        // Create a StringBuffer object with default initial capacity
        StringBuffer sb = new StringBuffer();

        // Print the current capacity
        System.out.println("Initial Capacity: " + sb.capacity());
    }
}

Output:

Initial Capacity: 16

Capacity Growth

As you append data to a StringBuffer, its capacity grows automatically when the current capacity is exceeded.

Example

public class StringBufferCapacityExample {
    public static void main(String[] args) {
        // Create a StringBuffer object with default initial capacity
        StringBuffer sb = new StringBuffer();

        // Append a string that exceeds the default capacity
        sb.append("This is a long string that exceeds the initial capacity of the StringBuffer.");

        // Print the new capacity
        System.out.println("New Capacity: " + sb.capacity());
    }
}

Output:

New Capacity: 70

Specified Initial Capacity

You can specify an initial capacity when creating a StringBuffer to avoid frequent reallocations if the final size is known in advance.

Example

public class StringBufferCapacityExample {
    public static void main(String[] args) {
        // Create a StringBuffer object with a specified initial capacity
        StringBuffer sb = new StringBuffer(50);

        // Print the specified initial capacity
        System.out.println("Specified Initial Capacity: " + sb.capacity());
    }
}

Output:

Specified Initial Capacity: 50

Conclusion

The StringBuffer.capacity() method in Java provides a way to check the current capacity of a StringBuffer object. By understanding and managing the capacity, you can optimize memory usage and performance in applications that involve extensive string manipulations. Whether you use the default capacity, rely on automatic growth, or specify an initial capacity, the capacity method helps you track and control the storage used by your StringBuffer.

Comments