Monday, September 17, 2012

StringBuilder() Constructor with no characters in it

public class StringBuilderConstructor {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
       
        //(a)
        System.out.println("Initial Capacity : " + sb.capacity());
        System.out.println("Initial Size : " + sb.length());
       
        sb.append("1234567890123456");
      
        //(b)
        System.out.println("Capacity After Sixteen Additions : " + sb.capacity());
        System.out.println("Size After Sixteen Additions : " + sb.length());
       
        sb.append("7");
      
        //(c)
        System.out.println("Capacity When it exceeds Sixteen Numbers : " + sb.capacity());
        System.out.println("Size When it exceeds Sixteen Numbers : " + sb.length());
       
        sb.append("12345678901234567");
      
        //(d)
        System.out.println("Capacity When it Equals ThirtyFour Numbers : " + sb.capacity());
        System.out.println("Size When it is less than or Equal to ThirtyFour Numbers : " + sb.length());       
   
    }
}

The  Answers Are :
----------------------
(a)
Initial Capacity : 16
       Initial Size : 0

 
(b)
Capacity After Sixteen Additions : 16
       Size After Sixteen Additions : 16
 
(c)
Capacity When it exceeds Sixteen Numbers : 34
       Size When it exceeds Sixteen Numbers : 17
 
(d)
Capacity When it exceeds ThirtyFour Numbers : 34
Size When it is less than or Equal to ThirtyFour Numbers : 34

No comments:

Post a Comment