Translate

Sunday 31 August 2014

String Buffer Class

String buffer objects are used to store character sequences but they are mutable objects which means that they can be altered directly.

Creating a String Buffer Object:-
StringBuffer s1 = null;
StringBuffer s2 = new StringBuffer();
StringBuffer s3 = new StringBuffer("JAVA");
StringBuffer s4 = new StringBuffer(1000);     // Size of string buffer is 1000

By default initial memory allocated to s2 is 16 characters.
For s3 it is 4+16 characters.
So when we create a StringBuffer object, by default memory is allocated for 16 characters. When we create a StringBuffer object and initialize it with string. Then memory allocated will be length of string and 16 characters more. We can also create object with specified memory.

StringBuffer s3 = new StringBuffer("JAVA");
System.out.println(s3.capacity);                // 4 + 16
System.out.println(s3.length);                    // 4

The capacity of StringBuffer object is no of characters it can hold at any instant and length is the number of character it is holding.

No comments:

Post a Comment

Total Pageviews