Java.lang.StringBuilder.trimToSize() Method

Advertisements


Description

The java.lang.StringBuilder.trimToSize() method attempts to reduce storage used for the character sequence.
If the buffer is larger than necessary to hold its current sequence of characters, then it may be resized to become more space efficient.

Declaration

Following is the declaration for java.lang.StringBuilder.trimToSize() method

public void trimToSize()

Parameters

  • NA

Return Value

This method does not return any value.

Exception

  • NA

Example

The following example shows the usage of java.lang.StringBuilder.trimToSize() method.

package com.tutorialspoint;

import java.lang.*;

public class StringBuilderDemo {

  public static void main(String[] args) {
  
    StringBuilder str = new StringBuilder("This is ");

    // append the string to StringBuilder
    str.append("Java Lang Package");

    // trimToSize string
    str.trimToSize();
    System.out.println("Programming :" + str);
  }
}

Let us compile and run the above program, this will produce the following result:

Programming :This is Java Lang Package



Advertisements
Advertisements