In this article, we will discuss difference between capacity() and length() methods of StringBuffer in detail with example
Often times, this two methods get confused as both deals with size but there are subtle difference between,
1. StringBuffer.capacity() v/s StringBuffer.length()
StringBuffer Method |
Description |
length(); | Returns current length or number of characters stored inside StringBuffer object |
capacity(); | Returns current capacity i.e.; amount of storage space available for newly inserted characters
Note: beyond the limit, new allocation occurs and it is overhead |
2. Example on length() and capacity() methods of StringBuffer:
Method Signature:
public int length();
public int capacity();
LengthAndCapacityMethodDifference.java
package in.bench.resources.sb.operation;
public class LengthAndCapacityMethodDifference {
public static void main(String[] args) {
// 1. create empty StringBuffer object
StringBuffer sbuffer1 = new StringBuffer();
// length and capacity for empty StringBuffer - 1
System.out.println("===== 1. Empty "
+ "StringBuffer object =====");
System.out.println("Length of empty StringBuffer : "
+ sbuffer1.length());
System.out.println("Capacity of empty StringBuffer : "
+ sbuffer1.capacity());
// 2. create StringBuffer object with initial content
StringBuffer sbuffer2 = new StringBuffer("Universe");
// length and capacity for StringBuffer - 2
System.out.println("\n\n===== 2. StringBuffer object"
+ " with initial content =====");
System.out.println("Length of StringBuffer : "
+ sbuffer2.length());
System.out.println("Capacity of StringBuffer : "
+ sbuffer2.capacity());
// 3. create StringBuffer object with initial size
StringBuffer sbuffer3 = new StringBuffer(65);
// length and capacity for StringBuffer - 3
System.out.println("\n\n===== 3. StringBuffer object"
+ " with initial size =====");
System.out.println("Length of StringBuffer : "
+ sbuffer3.length());
System.out.println("Capacity of StringBuffer : "
+ sbuffer3.capacity());
// 4. create StringBuffer with initial size
// and append some content
StringBuffer sbuffer4 = new StringBuffer(35);
sbuffer4.append("HelloWorld");
// length and capacity for StringBuffer - 3
System.out.println("\n\n===== 4. StringBuffer"
+ " with initial size and some content =====");
System.out.println("Length of StringBuffer : "
+ sbuffer4.length());
System.out.println("Capacity of StringBuffer : "
+ sbuffer4.capacity());
}
}
Output:
===== 1. Empty StringBuffer object =====
Length of empty StringBuffer : 0
Capacity of empty StringBuffer : 16
===== 2. StringBuffer object with initial content =====
Length of StringBuffer : 8
Capacity of StringBuffer : 24
===== 3. StringBuffer object with initial size =====
Length of StringBuffer : 0
Capacity of StringBuffer : 65
===== 4. StringBuffer with initial size and some content =====
Length of StringBuffer : 10
Capacity of StringBuffer : 35
Explanation:
- First created empty StringBuffer has a default capacity of 16, which doesn’t contain any value in it. Therefore, its length is 0 and capacity is 16
- For second scenario, where we have explicitly passed constructor-argument “Universe” which is 8 characters long in length. Therefore, its length is 8 and notice this time capacity has been increased to 24 because of summation of initial capacity of 16 plus initial string passed inside constructor-argument i.e.; 8
- But in 3rd case, we have suppressed initial capacity by passing explicit integer-value of 65 and it doesn’t contain initial value. Therefore, its length is 0 and capacity is the value passed to constructor-argument i.e.; 65
- 4th case is very interesting where we have suppressed both initial capacity and initial string by appending to it. Therefore, its length is 10 and capacity is the value passed to constructor-argument i.e.; 35.
Note: In case, initial string + appended string’s length is more than capacity, then capacity increases. Try it.
Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.
Related Articles:
- StringBuffer class
- StringBuffer append() method (13)
- StringBuffer capacity() method
- StringBuffer charAt(int index) method
- StringBuffer delete(int start, int end) method
- StringBuffer deleteCharAt(int index) method
- StringBuffer ensureCapacity(int minimumCapacity) method
- StringBuffer indexOf() method (2)
- StringBuffer insert() method (12)
- StringBuffer lastIndexOf() method (2)
- StringBuffer length() method
- StringBuffer replace(int start, int end, String str) method
- StringBuffer reverse() method
- StringBuffer substring(int start, int end) method
References:
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
- https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
- https://docs.oracle.com/javase/tutorial/java/data/strings.html
Happy Coding !!
Happy Learning !!