Tuesday, September 18, 2012

StringBuilder Constructor with default argument as a String variable (i.e), initialized to the contents of the specified string, public StringBuilder(String str)

StringBuilder(String str)
Constructs a string builder initialized to the contents of the specified string.
The initial capacity of the string builder is 16 plus the length of the string argument.

Parameters:
str - the initial contents of the buffer.
Throws:
NullPointerException - if str is null

public class StringBuilderConstructorofSpecifiedString {
    public static void main(String[] args) {

        StringBuilder sb1 = new StringBuilder("1");
        System.out.println("Initial Capacity of sb1 : " + sb1.capacity());
        System.out.println("Initial Size of sb1 : " + sb1.length());       
       
        sb1.append("2");
        System.out.println("Capacity of sb1 After 2 Addition  : " + sb1.capacity());
        System.out.println("Size of sb1 After 2 Addition : " + sb1.length());       
       
        sb1.append("123456789012345");
        System.out.println("Capacity of sb1 When it Equals Seventeen Numbers : " + sb1.capacity());
        System.out.println("Size of sb1 When it Equals Seventeen Numbers : " + sb1.length());       

        sb1.append("6");
        System.out.println("Capacity of sb1 When it exceeds Seventeen Numbers : " + sb1.capacity());
        System.out.println("Size of sb1 When it exceeds Seventeen Numbers : " + sb1.length());           
       
        StringBuilder sb2 = new StringBuilder("");
        System.out.println("Initial Capacity of sb2 : " + sb2.capacity());
        System.out.println("Initial Size of sb2 : " + sb2.length());

        sb2.append("1");
        System.out.println("Capacity of sb2 After One Additions : " + sb2.capacity());
        System.out.println("Size of sb2 After One Additions : " + sb2.length());
       
        sb2.append("1234567890123456");
        System.out.println("Capacity of sb2 When it exceeds Sixteen Numbers : " + sb2.capacity());
        System.out.println("Size of sb2 When it exceeds Sixteen Numbers : " + sb2.length());
       
        sb2.append("1234567890123456789");
        System.out.println("Capacity of sb2 When it exceeds ThirtyFour Numbers : " + sb2.capacity());
        System.out.println("Size of sb2 When it is less than or Equal to ThirtyFour Numbers:"+sb2.length());          
    }
}

The  Answers Are :
----------------------
(a1)
Initial Capacity of sb1 : 17
        Initial Size of sb1 : 1

(b1)


Capacity of sb1 After 2 Addition  : 17
        Size of sb1 After 2 Addition  :
2
(c1)
Capacity of sb1 When it Equals Seventeen Numbers : 17
        Size of sb1 When it Equals Seventeen Numbers : 17

(d1)

Capacity of sb1 When it exceeds Seventeen Numbers : 36
        Size of sb1 When it exceeds Seventeen Numbers : 18

 

(a2)
Initial Capacity of sb2 : 16
        Initial Size of sb2 : 0

(b2)

Capacity of sb2 After One Additions : 16
        Size of sb2 After One Additions : 1

(c2)

Capacity of sb2 When it exceeds Sixteen Numbers : 34
        Size of sb2 When it exceeds Sixteen Numbers : 17

(d2)

Capacity of sb2 When it exceeds ThirtyFour Numbers : 70
Size of sb2 When it is less than or Equal to ThirtyFour Numbers : 36

 

No comments:

Post a Comment