Skip to content

Instantly share code, notes, and snippets.

@RashidJorvee
Last active January 5, 2019 15:19
Show Gist options
  • Save RashidJorvee/17e92f0040b28388e7e37f9981a376fe to your computer and use it in GitHub Desktop.
Save RashidJorvee/17e92f0040b28388e7e37f9981a376fe to your computer and use it in GitHub Desktop.
How to use trimToSize() method in Java?
package rashid.jorvee;
public class TrimToSize {
public static void main(String[] args) {
StringBuffer copyText=new StringBuffer();
copyText.append("trim to size method in java");
System.out.println(copyText +", and length of StringBuffer copyText is:" +copyText.length());
System.out.println("Capacity of copyText is::"+copyText.capacity());
copyText.trimToSize();
System.out.println(copyText +", and length of StringBuffer copyText after trimToSize() is:" +copyText.length());
System.out.println("Capacity of copyText after trimToSize() is::"+copyText.capacity());
}
}
@RashidJorvee
Copy link
Author

RashidJorvee commented Jan 5, 2019

Output of this program:

trim to size method in java, and length of StringBuffer copyText is:27
Capacity of copyText is::34
trim to size method in java, and length of StringBuffer copyText after trimToSize()  is:27
Capacity of copyText after trimToSize() is::27

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment