Java StringBuilder capacity() Method

The StringBuilder.capacity() method in Java is used to retrieve the current capacity of a StringBuilder 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
    • Retrieving Initial Capacity
    • Ensuring Capacity
    • Modifying the Capacity
  4. Conclusion

Introduction

The StringBuilder.capacity() method is a member of the StringBuilder class in Java. It returns the current capacity of the StringBuilder object, which is the amount of storage available for newly inserted characters, without reallocating the internal buffer.

capacity Method Syntax

The syntax for the capacity method is as follows:

public int capacity()

This method does not take any parameters and returns an integer representing the current capacity of the StringBuilder object.

Examples

Retrieving Initial Capacity

The capacity method can be used to check the initial capacity of a newly created StringBuilder.

Example

public class StringBuilderCapacityExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();

        int initialCapacity = sb.capacity();

        System.out.println("Initial capacity: " + initialCapacity);
    }
}

Output:

Initial capacity: 16

Ensuring Capacity

You can ensure that the StringBuilder has a certain minimum capacity using the ensureCapacity method, and then retrieve the capacity.

Example

public class StringBuilderCapacityExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();

        sb.ensureCapacity(50);
        int ensuredCapacity = sb.capacity();

        System.out.println("Ensured capacity: " + ensuredCapacity);
    }
}

Output:

Ensured capacity: 50

Modifying the Capacity

When you append data to a StringBuilder, its capacity may automatically increase if the current capacity is exceeded.

Example

public class StringBuilderCapacityExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder(10);

        System.out.println("Initial capacity: " + sb.capacity());

        sb.append("Hello, World!");
        System.out.println("Capacity after append: " + sb.capacity());
    }
}

Output:

Initial capacity: 10
Capacity after append: 22

In this example, the initial capacity of the StringBuilder is 10. After appending the string "Hello, World!", the capacity increases to accommodate the new data.

Conclusion

The StringBuilder.capacity() method in Java is used for managing the internal buffer of a StringBuilder object. By understanding how to use this method, you can effectively monitor and control the memory allocation for your string manipulations. Whether you need to check the initial capacity, ensure a minimum capacity, or observe how the capacity changes with appended data, the capacity method provides valuable insight into the internal workings of the StringBuilder class.

Comments