🎓 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
StringBuffer.capacity() method in Java is used to return the current capacity of the StringBuffer object. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
capacityMethod Syntax- Examples
- Initial Capacity
- Capacity Growth
- Specified Initial Capacity
- Conclusion
Introduction
The StringBuffer.capacity() method is a member of the StringBuffer class in Java. The capacity of a StringBuffer object represents the amount of storage available for newly appended characters, beyond which the buffer will need to be reallocated. Understanding and managing the capacity of a StringBuffer can help optimize memory usage and improve performance in scenarios that involve extensive string manipulation.
capacity Method Syntax
The syntax for the capacity method is as follows:
public synchronized int capacity()
This method returns the current capacity of the StringBuffer object.
Examples
Initial Capacity
When a StringBuffer is created without specifying an initial capacity, it uses a default capacity.
Example
public class StringBufferCapacityExample {
public static void main(String[] args) {
// Create a StringBuffer object with default initial capacity
StringBuffer sb = new StringBuffer();
// Print the current capacity
System.out.println("Initial Capacity: " + sb.capacity());
}
}
Output:
Initial Capacity: 16
Capacity Growth
As you append data to a StringBuffer, its capacity grows automatically when the current capacity is exceeded.
Example
public class StringBufferCapacityExample {
public static void main(String[] args) {
// Create a StringBuffer object with default initial capacity
StringBuffer sb = new StringBuffer();
// Append a string that exceeds the default capacity
sb.append("This is a long string that exceeds the initial capacity of the StringBuffer.");
// Print the new capacity
System.out.println("New Capacity: " + sb.capacity());
}
}
Output:
New Capacity: 70
Specified Initial Capacity
You can specify an initial capacity when creating a StringBuffer to avoid frequent reallocations if the final size is known in advance.
Example
public class StringBufferCapacityExample {
public static void main(String[] args) {
// Create a StringBuffer object with a specified initial capacity
StringBuffer sb = new StringBuffer(50);
// Print the specified initial capacity
System.out.println("Specified Initial Capacity: " + sb.capacity());
}
}
Output:
Specified Initial Capacity: 50
Conclusion
The StringBuffer.capacity() method in Java provides a way to check the current capacity of a StringBuffer object. By understanding and managing the capacity, you can optimize memory usage and performance in applications that involve extensive string manipulations. Whether you use the default capacity, rely on automatic growth, or specify an initial capacity, the capacity method helps you track and control the storage used by your StringBuffer.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment