🎓 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
StringBuilder.capacity() method in Java is used to retrieve the current capacity of a StringBuilder 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
- Retrieving Initial Capacity
- Ensuring Capacity
- Modifying the Capacity
- Conclusion
Introduction
The StringBuilder.capacity() method is a member of the StringBuilder class in Java. It returns the current capacity of the StringBuilder object, which is the amount of storage available for newly inserted characters, without reallocating the internal buffer.
capacity Method Syntax
The syntax for the capacity method is as follows:
public int capacity()
This method does not take any parameters and returns an integer representing the current capacity of the StringBuilder object.
Examples
Retrieving Initial Capacity
The capacity method can be used to check the initial capacity of a newly created StringBuilder.
Example
public class StringBuilderCapacityExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
int initialCapacity = sb.capacity();
System.out.println("Initial capacity: " + initialCapacity);
}
}
Output:
Initial capacity: 16
Ensuring Capacity
You can ensure that the StringBuilder has a certain minimum capacity using the ensureCapacity method, and then retrieve the capacity.
Example
public class StringBuilderCapacityExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
sb.ensureCapacity(50);
int ensuredCapacity = sb.capacity();
System.out.println("Ensured capacity: " + ensuredCapacity);
}
}
Output:
Ensured capacity: 50
Modifying the Capacity
When you append data to a StringBuilder, its capacity may automatically increase if the current capacity is exceeded.
Example
public class StringBuilderCapacityExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder(10);
System.out.println("Initial capacity: " + sb.capacity());
sb.append("Hello, World!");
System.out.println("Capacity after append: " + sb.capacity());
}
}
Output:
Initial capacity: 10
Capacity after append: 22
In this example, the initial capacity of the StringBuilder is 10. After appending the string "Hello, World!", the capacity increases to accommodate the new data.
Conclusion
The StringBuilder.capacity() method in Java is used for managing the internal buffer of a StringBuilder object. By understanding how to use this method, you can effectively monitor and control the memory allocation for your string manipulations. Whether you need to check the initial capacity, ensure a minimum capacity, or observe how the capacity changes with appended data, the capacity method provides valuable insight into the internal workings of the StringBuilder class.
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