Java StringBuilder trimToSize() method attempts to reduce storage used for the character sequence.
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
Free Spring Boot Tutorial - 5 Hours Full Course
Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course