Java StringBuilder trimToSize() Method

The StringBuilder.trimToSize() method in Java is used to trim the capacity of the StringBuilder object to the current size of the character sequence.

Table of Contents

  1. Introduction
  2. trimToSize Method Syntax
  3. Examples
    • Trimming Capacity to Size
    • Checking Capacity Before and After Trimming
  4. Real-World Use Case
  5. Conclusion

Introduction

The StringBuilder.trimToSize() method is a member of the StringBuilder class in Java. It is used to reduce the capacity of the StringBuilder object to match the current length of the character sequence. This can be useful for optimizing memory usage, especially after a large reduction in the size of the content.

trimToSize() Method Syntax

The syntax for the trimToSize method is as follows:

public void trimToSize()

This method does not take any parameters and does not return any value. It adjusts the capacity of the StringBuilder object to be equal to its current length.

Examples

Trimming Capacity to Size

The trimToSize method can be used to reduce the capacity of a StringBuilder to match its current length.

Example

public class StringBuilderTrimToSizeExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder(50); // Initial capacity of 50
        sb.append("Hello, World!");

        // Print initial capacity and length
        System.out.println("Initial capacity: " + sb.capacity());
        System.out.println("Initial length: " + sb.length());

        // Trim to size
        sb.trimToSize();

        // Print capacity and length after trimming
        System.out.println("Capacity after trimToSize: " + sb.capacity());
        System.out.println("Length after trimToSize: " + sb.length());
    }
}

Output:

Initial capacity: 50
Initial length: 13
Capacity after trimToSize: 13
Length after trimToSize: 13

Checking Capacity Before and After Trimming

You can check the capacity of the StringBuilder before and after calling the trimToSize method to see how the capacity changes.

Example

public class StringBuilderTrimToSizeExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder(100); // Initial capacity of 100
        sb.append("Java Programming");

        // Print initial capacity and length
        System.out.println("Initial capacity: " + sb.capacity());
        System.out.println("Initial length: " + sb.length());

        // Trim to size
        sb.trimToSize();

        // Print capacity and length after trimming
        System.out.println("Capacity after trimToSize: " + sb.capacity());
        System.out.println("Length after trimToSize: " + sb.length());
    }
}

Output:

Initial capacity: 100
Initial length: 16
Capacity after trimToSize: 16
Length after trimToSize: 16

In this example, the initial capacity of the StringBuilder is 100. After calling trimToSize(), the capacity is reduced to match the current length of 16 characters.

Real-World Use Case

Example: Optimizing Memory Usage

In a real-world scenario, you might use trimToSize to optimize memory usage after constructing a large string and then significantly reducing its size. This can help free up unused memory.

Example Code

public class MemoryOptimizationExample {
    public static void main(String[] args) {
        // Create a StringBuilder with a large initial capacity
        StringBuilder sb = new StringBuilder(1000);
        sb.append("This is a very large initial string that we will reduce.");

        // Simulate reducing the size of the content
        sb.delete(10, sb.length());

        // Print capacity and length before trimming
        System.out.println("Capacity before trimToSize: " + sb.capacity());
        System.out.println("Length before trimToSize: " + sb.length());

        // Trim to size to optimize memory usage
        sb.trimToSize();

        // Print capacity and length after trimming
        System.out.println("Capacity after trimToSize: " + sb.capacity());
        System.out.println("Length after trimToSize: " + sb.length());
    }
}

Output:

Capacity before trimToSize: 1000
Length before trimToSize: 10
Capacity after trimToSize: 10
Length after trimToSize: 10

Conclusion

The StringBuilder.trimToSize() method in Java is used for optimizing memory usage by reducing the capacity of a StringBuilder object to match its current length. By understanding how to use this method, you can efficiently manage the memory allocated for your StringBuilder objects. Whether you need to trim the capacity to size or check the capacity before and after trimming, the trimToSize method provides a reliable solution for these tasks.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top