Академический Документы
Профессиональный Документы
Культура Документы
1
StringBuffer class in Java
• Peer class of String
• String represents fixed length and immutable
character sequence
• StringBuffer allows growable and writable character
sequence
• Characters in StringBuffer can be
inserted/appended/added/deleted any where and the
size of the StringBuffer will automatically
grow/shrink to make room
2
StringBuffer Constructors
• StringBuffer()
<< Reserves room for 16 characters >>
• StringBuffer(int size)
<< Explicitly sets the size of buffer >>
• StringBuffer(String str)
<< Sets the initial content of the string
Buffer and allocates room for 16 more
characters>>
4
void ensureCapacity(int capacity)
• Useful if you know in advance the size of buffer
• Used to preallocate room for a certain number of characters
after StringBuffer object has been created.
• <<capacity>> specifies the size of buffer
strbuf.ensureCapacity(-20); NO EFFECT ON
strbuf1.ensureCapacity(-30); CAPACITY
6
StringBuffer s1 = new StringBuffer("Java");
System.out.println(s1.length()); 4
System.out.println(s1.capacity()); 20
s1.ensureCapacity(50);
System.out.println(s1.length()); 4
System.out.println(s1.capacity()); 50
7
void setLength(int len)
8
StringBuffer strbuf = new StringBuffer();
System.out.println(strbuf.length()); 0
System.out.println(strbuf.capacity());16
strbuf.setLength(20);
System.out.println(strbuf.length()); 20
System.out.println(strbuf.capacity()); 34
10
Appending [Adding at the End] String Buffer
11
Append Example
class Circle
{
private double radius;
Circle(double radius)
{
this.radius = radius;
}
} class CircleTest
{
public static void main(String args[])
{
StringBuffer strbuf = new StringBuffer("Object");
strbuf.append(" oriented");
System.out.println(strbuf);
Object oriented
strbuf.append(6.1);
System.out.println(strbuf); Object oriented6.1
12
strbuf.append(6.1);
System.out.println(strbuf); Object oriented6.16.1
strbuf.append(c1);
System.out.println(strbuf);
} Object oriented6.16.1Circle@82ba41
}
Hashcode of Circle
13
class Circle
{
private double radius;
Circle(double radius)
{
this.radius = radius;
}
public String toString()
{
return "Circle with Radius:"+radius;
}
} OUTPUT
class CircleTest
JavaCircle with Radius:10.56
{
public static void main(String args[])
{
StringBuffer strbuf = new StringBuffer("Java");
Circle c1 = new Circle(10.56);
strbuf.append(c1);
System.out.println(strbuf);
} 14
}
Inserting charcters
• Insert() method can be used for inserting
characters
• Insert() method is also overloaded
1. StringBuffer insert(int index , String str);
2. StringBuffer insert(int index , char ch);
3. StringBuffer insert(int index, Object obj);
• << index >> must be within permitted range and
should be positive
15
StringBuffer strbuf = new StringBuffer("Java");
strbuf.insert(3," "); Jav a
System.out.println(strbuf);
17
Deleting Characters
Deleting a single charcater
• To delete a single character use deleteCharAt() method
• Syntax deleteCharAt() :
StringBuffer deleteCharAt(int loc)
<< Deletes a character from index indicated by loc>>
• <<loc>> should be positive and within permitted range.