BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java StringBuilder trimToSize()

By Chaitanya Singh | Filed Under: java

Java StringBuilder trimToSize() method is used to reduce the capacity of StringBuilder instance, if possible. This method checks for non-utilized allocated space, it frees up the storage to optimize the buffer size. This method belongs to the StringBuilder class in Java.

The syntax of trimToSize() method is:

sb.trimToSize() //free up non-utilized buffer

Here, sb is an object of StringBuilder class.

trimToSize() Description

public void trimToSize(): It optimizes the buffer memory by reducing the space that is currently allocated to the current StringBuilder instance.

trimToSize() Parameters

  • It does not take any parameter.

trimToSize() Return Value

  • The return type of this method is void as it does not return anything.

Example of trimToSize() Method

public class JavaExample {
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder();
    System.out.println("Default Capacity: "+sb.capacity());

    //append string
    sb.append("BeginnersBook");
    System.out.println("String appended: "+sb);
    
    //cleanup buffer
    sb.trimToSize();
    System.out.println("Capacity after trim: "+sb.capacity());
  }
}

Output:

Java StringBuilder trimToSize() Output_1

Example 2 of trimToSize() Method

public class JavaExample {
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder();
    System.out.println("Default Capacity: "+sb.capacity());

    //If current capacity is less than 17 then it will increase
    //old capacity = (new capacity * 2) +2
    sb.ensureCapacity(17); //new = 16*2 +2 =34
    System.out.println("Capacity after ensureCapacity: "+sb.capacity());

    //append string
    sb.append("Welcome");
    System.out.println("String appended: "+sb);

    //cleanup buffer
    sb.trimToSize();
    System.out.println("Capacity after trim: "+sb.capacity());
  }
}

Output:

Java StringBuilder trimToSize() Output_2

Recommended Posts

  • Java StringBuilder capacity()
  • Java StringBuilder ensureCapacity()
  • Java StringBuilder substring()
  • Java StringBuilder charAt()

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap