Java StringBuffer trimToSize() Method

The StringBuffer.trimToSize() method in Java is used to reduce the capacity of the StringBuffer object to the current length. 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. trimToSize Method Syntax
  3. Examples
    • Trimming the Capacity to Size
    • Before and After Trimming
  4. Conclusion

Introduction

The trimToSize() method is a member of the StringBuffer class in Java. It allows you to reduce the capacity of the StringBuffer to the current length of the character sequence. This is useful for optimizing memory usage by removing any unused capacity that may have been allocated.

trimToSize Method Syntax

The syntax for the trimToSize method is as follows:

public synchronized void trimToSize()

Parameters:

  • None

Returns:

  • Void

Examples

Trimming the Capacity to Size

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

Example

public class StringBufferTrimToSizeExample {
    public static void main(String[] args) {
        // Create a StringBuffer object with initial capacity and content
        StringBuffer sb = new StringBuffer(50);
        sb.append("Hello, World!");

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

        // Trim the capacity to the size of the content
        sb.trimToSize();

        // Print the capacity and length after trimming
        System.out.println("Trimmed capacity: " + sb.capacity());
        System.out.println("Trimmed length: " + sb.length());
    }
}

Output:

Initial capacity: 50
Initial length: 13
Trimmed capacity: 13
Trimmed length: 13

Before and After Trimming

This example demonstrates the difference in capacity before and after using the trimToSize method.

Example

public class StringBufferTrimToSizeExample {
    public static void main(String[] args) {
        // Create a StringBuffer object with initial capacity and content
        StringBuffer sb = new StringBuffer(100);
        sb.append("Java Programming");

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

        // Trim the capacity to the size of the content
        sb.trimToSize();

        // Print the capacity and length after trimming
        System.out.println("Trimmed capacity: " + sb.capacity());
        System.out.println("Trimmed length: " + sb.length());
    }
}

Output:

Initial capacity: 100
Initial length: 16
Trimmed capacity: 16
Trimmed length: 16

Conclusion

The StringBuffer.trimToSize() method in Java provides a way to optimize the memory usage of a StringBuffer object by reducing its capacity to the current length of the character sequence. By understanding how to use this method, you can ensure that your StringBuffer objects are not using more memory than necessary. This method is particularly useful for applications that need to manage memory efficiently, especially when dealing with large amounts of dynamic string data.

Comments