🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The StringBuilder.trimToSize() method in Java is used to trim the capacity of the StringBuilder object to be equal to the current length of the character sequence. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.
Table of Contents
- Introduction
trimToSizeMethod Syntax- Examples
- Trimming Capacity to Size
- Checking Capacity Before and After Trimming
- Conclusion
Introduction
The StringBuilder.trimToSize() method is a member of the StringBuilder class in Java. It allows you to reduce the capacity of the StringBuilder object to match its current length. This can be useful for optimizing memory usage when you no longer need the extra capacity that was initially allocated.
trimToSize Method Syntax
The syntax for the trimToSize method is as follows:
public void trimToSize()
This method does not take any parameters and does not return any value. It adjusts the capacity of the StringBuilder object to be equal to its current length.
Examples
Trimming Capacity to Size
The trimToSize method can be used to reduce the capacity of a StringBuilder to match its current length.
Example
public class StringBuilderTrimToSizeExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder(50); // Initial capacity of 50
sb.append("Hello, World!");
System.out.println("Initial capacity: " + sb.capacity());
sb.trimToSize();
System.out.println("Capacity after trimToSize: " + sb.capacity());
}
}
Output:
Initial capacity: 50
Capacity after trimToSize: 13
Checking Capacity Before and After Trimming
You can check the capacity of the StringBuilder before and after calling the trimToSize method to see how the capacity changes.
Example
public class StringBuilderTrimToSizeExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder(100); // Initial capacity of 100
sb.append("Java Programming");
System.out.println("Initial capacity: " + sb.capacity());
System.out.println("Length: " + sb.length());
sb.trimToSize();
System.out.println("Capacity after trimToSize: " + sb.capacity());
System.out.println("Length after trimToSize: " + sb.length());
}
}
Output:
Initial capacity: 100
Length: 16
Capacity after trimToSize: 16
Length after trimToSize: 16
In this example, the initial capacity of the StringBuilder is 100. After calling trimToSize(), the capacity is reduced to match the current length of 16 characters.
Conclusion
The StringBuilder.trimToSize() method in Java is used for optimizing memory usage by reducing the capacity of a StringBuilder object to match its current length. By understanding how to use this method, you can efficiently manage the memory allocated for your StringBuilder objects. Whether you need to trim the capacity to size or check the capacity before and after trimming, the trimToSize method provides a reliable solution for these tasks.
Comments
Post a Comment
Leave Comment