Exploring StringBuffer ensureCapacity() Method in Java: Usage and Examples
Table of Content:
If you want to preallocate room for a certain number of characters after a StringBuffer has been constructed, you can use ensureCapacity( ) to set the size of the buffer. This is useful if you know in advance that you will be appending a large number of small strings to a StringBuffer.
Syntax
public void ensureCapacity(int minimumCapacity)
Ensures that the capacity is at least equal to the specified minimum. If the current capacity is less than the argument, then a new internal array is allocated with greater capacity. The new capacity is the larger of:
- The
minimumCapacity
argument. - Twice the old capacity, plus
2
.
If the minimumCapacity
argument is nonpositive, this method takes no action and simply returns.
Parameters
minimumCapacity - the minimum desired capacity.
Program
public class MethodensureCapacity { public static void main(String args[]) { StringBuffer sb1 = new StringBuffer("ok"); System.out.println("sb1 default capacity: " + sb1.capacity()); // 18 sb1.ensureCapacity(10); System.out.println("sb1.ensureCapacity(10): " + sb1.capacity()); // 18 StringBuffer sb2 = new StringBuffer("abcdef"); // with some length, say 6 System.out.println("sb2 capacity with six characters size: " + sb2.capacity()); // 22 sb2.ensureCapacity(30); System.out.println("sb2.ensureCapacity(30): " + sb2.capacity()); // 46 // with length 0 StringBuffer sb3 = new StringBuffer(); System.out.println("sb3 default capacity: " + sb3.capacity()); // 16 sb3.ensureCapacity(50); System.out.println("sb3.ensureCapacity(50): " + sb3.capacity()); // 50 // negative value given StringBuffer sb4 = new StringBuffer(); System.out.println("sb4 default capacity: " + sb4.capacity()); // 16 sb4.ensureCapacity(-50); System.out.println("sb3.ensureCapacity(-50): " + sb4.capacity()); // 16 } }
Output
sb1 default capacity: 18 sb1.ensureCapacity(10): 18 sb2 capacity with six characters size: 22 sb2.ensureCapacity(30): 46 sb3 default capacity: 16 sb3.ensureCapacity(50): 50 sb4 default capacity: 16 sb3.ensureCapacity(-50): 16 Press any key to continue . . .
The ensure capacity cannot be less than the old capacity.
StringBuffer sb1 = new StringBuffer(); System.out.println("sb1 default capacity: " + sb1.capacity()); // 16 sb1.ensureCapacity(10); System.out.println("sb1.ensureCapacity(10): " + sb1.capacity()); // 16
The ensure capacity is, now in this case, twice the old capacity plus 2 (but not 30).
StringBuffer sb2 = new StringBuffer("abcdef"); // length is 6 System.out.println("sb2 capacity with six characters size: " + sb2.capacity()); // 22 sb2.ensureCapacity(30); System.out.println("sb2.ensureCapacity(30): " + sb2.capacity()); // 46
When ensure capacity is more than the default 16, the ensure capacity will be 50
StringBuffer sb3 = new StringBuffer(); System.out.println("sb3 default capacity: " + sb3.capacity()); // 16 sb3.ensureCapacity(50); System.out.println("sb3.ensureCapacity(50): " + sb3.capacity()); // 50
The negative capacity value does not have any effect, just return the old existing value.
StringBuffer sb4 = new StringBuffer(); System.out.println("sb4 default capacity: " + sb4.capacity()); // 16 sb4.ensureCapacity(-50); System.out.println("sb3.ensureCapacity(-50): " + sb4.capacity()); // 16
Another Program
class StringBufferExample { public static void main(String args[]){ StringBuffer sb=new StringBuffer(); System.out.println(sb.capacity());//default 16 sb.append("Hello"); System.out.println(sb.capacity());//now 16 sb.append("java is my favourite language"); System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2 sb.ensureCapacity(10);//now no change System.out.println(sb.capacity());//now 34 sb.ensureCapacity(50);//now (34*2)+2 System.out.println(sb.capacity());//now 70 } }
Output
16 16 34 34 70 Press any key to continue . . .