Java StringBuilder trimToSize() Method Example

Java StringBuilder trimToSize() method attempts to reduce storage used for the character sequence.

Java StringBuilder trimToSize() Method Example

public class TrimToSizeExample {
    public static void main(String[] args) {
        StringBuilder builder = new StringBuilder("javaguides ");
        System.out.println(builder.capacity());
        builder.trimToSize();
        System.out.println(builder.capacity());
    }
}
Output:
27
11

Reference



Comments