How StringBuffer maintains capacity?
newCapacity = string.length() * 2 + 2;
public class StringDemo {
public static void main(String[] args) {
stringBufferDemo();
}
private static void stringBufferDemo(){
StringBuilder sbb;
StringBuffer sb = new StringBuffer();
System.out.println("Length : " + sb.length());
System.out.println("Capacity : " + sb.capacity());
sb.ensureCapacity(17);
System.out.println("Capacity : " + sb.capacity());
sb.ensureCapacity(35);
System.out.println("Capacity : " + sb.capacity());
sb.ensureCapacity(71);
System.out.println("Capacity : " + sb.capacity());
}
}
Output:
Length : 0
Capacity : 16
Capacity : 34
Capacity : 70
Capacity : 142