String Buffer
String Buffer
StringBuffer (Mutable)
• StringBuffer is a peer class of String that provides
much of the functionality of strings.
• As you know, String represents fixed-length,
immutable character sequences. In contrast,
• StringBuffer represents growable and writable
character sequences.(MUTABLE)
• StringBuffer may have characters and substrings
inserted in the middle or appended to the end.
• StringBuffer will automatically grow to make room
for such additions and often has more characters
preallocated than are actually needed, to allow room
for growth.
StringBuffer Constructors
StringBuffer defines these four constructors:
1.StringBuffer( ) - 16 characters
StringBuffer s = new StringBuffer( ) ;
2.StringBuffer(int size) – externally defined size
StringBuffer s = new StringBuffer(10 ) ;
3.StringBuffer(String str) – initial content + 16 chars
StringBuffer s = new StringBuffer( “Welcome”) ;
4.StringBuffer(CharSequence chars) - initial content
+ 16 chars
CharSequence cs= “Hi GOAT”
StringBuffer s = new StringBuffer(cs) ;
length( ) and capacity( )
• int length( )- The current length of a StringBuffer can be
found via the length( ) method,
• int capacity( ) - while the total allocated capacity can be
found through the capacity( ) method
class StringBufferDemo
{
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Hello");
System.out.println("buffer = " + sb);
System.out.println("length = " + sb.length());
System.out.println("capacity = " + sb.capacity());
Output:
buffer = Hello
length = 5
capacity = 21
ensureCapacity( )
• 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
• void ensureCapacity(int minCapacity)
• By setting the capacity in advance, you can
reduce the number of times the StringBuffer
needs to allocate more memory, which can
improve performance.
public class EnsureCapacityExample {
public static void main(String[] args) {
// Create a StringBuffer with initial content
StringBuffer sb = new StringBuffer("Hello");
// Print initial capacity
System.out.println("Initial capacity: " + sb.capacity());
// Ensure the capacity is at least 50
sb.ensureCapacity(50);
// Print capacity after ensuring
System.out.println("Capacity after ensuring: " + sb.capacity());
// Append a large number of small strings
for (int i = 0; i < 10; i++) {
sb.append(" World");
}
// Print the final content and capacity
System.out.println("Final content: " + sb);
System.out.println("Final capacity: " + sb.capacity());}}
Output:
Initial capacity: 21
Capacity after ensuring:
50
Final content: Hello World World World
World World World World World World
World
Final capacity: 65
setLength( )
// Use getChars to copy characters from the StringBuffer to the target array
sb.getChars(sourceStart, sourceEnd, target, targetStart);
Output:
Target array content: 0000World000000000000
append( )
• append( ) method concatenates the string representation of any
other type of data to the end of the invoking StringBuffer object.
• It has several overloaded versions.
Output:
After delete: This a test.
After deleteCharAt: his a test.
replace( )
Output:
Comparing "apple" with "banana": -1
Comparing "apple" with "apple": 0
Comparing "apple" with "Apple": 32
Comparing "banana" with "Apple": 31
String Comparison
Bubble Sort
class SortString
{
static String arr[] = { "Now", "is", "the", "time", "for", "all", "good", "men", "to", "come", "to", "the", "aid",
"of", "their", "country“ };
public static void main(String args[])
{
for(int j = 0; j < arr.length; j++)
{
for(int i = j + 1; i < arr.length; i++)
{
if(arr[i].compareTo(arr[j]) < 0)
{
String t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}
}
for (int j = 0; j < arr.length; j++)
{
System.out.println(arr[j]);
}
}
}
}
The output of this program is the list of words:
Now
aid
all
come
country
for
good
is
men
of
the
the
their
time
to
to