380 likes | 1.22k Views
StringBuffer Class In Java. StringBuffer class in Java. Peer class of String String represents fixed length and immutable character sequence StringBuffer allows growable and writable character sequence
E N D
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
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>> Note : At the time of creation if no size is specified then length will be 0 but capacity will be 16
int length()int capacity() • int length() returns the current length of string buffer • int capacity() returns the allocated capacity • Example : StringBuffer strbuf = new StringBuffer(); System.out.println(strbuf.length()); System.out.println(strbuf.capacity()); StringBuffer strbuf1 = new StringBuffer("Object"); System.out.println(strbuf1.length()); System.out.println(strbuf1.capacity()); No size Mentioned 0 16 No size Mentioned 6 22
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 StringBuffer strbuf = new StringBuffer(); System.out.println(strbuf.length()); System.out.println(strbuf.capacity()); strbuf.ensureCapacity(10); System.out.println(strbuf.capacity()); StringBuffer strbuf1 = new StringBuffer("Object"); System.out.println(strbuf1.length()); System.out.println(strbuf1.capacity()); strbuf1.ensureCapacity(20); System.out.println(strbuf1.capacity()); 0 16 16 6 22 22
StringBuffer strbuf = new StringBuffer(); System.out.println(strbuf.length()); System.out.println(strbuf.capacity()); strbuf.ensureCapacity(20); System.out.println(strbuf.capacity()); StringBuffer strbuf1 = new StringBuffer("Object"); System.out.println(strbuf1.length()); System.out.println(strbuf1.capacity()); strbuf1.ensureCapacity(30); System.out.println(strbuf1.capacity()); 0 16 34 6 22 46 What will happen for the following statements strbuf.ensureCapacity(-20); strbuf1.ensureCapacity(-30); NO EFFECT ON CAPACITY
StringBuffer s1 = new StringBuffer("Java"); System.out.println(s1.length()); System.out.println(s1.capacity()); s1.ensureCapacity(50); System.out.println(s1.length()); System.out.println(s1.capacity()); 4 20 4 50
char charAt(int where)void setCharAt(int where, char ch) • charAt() method is same as String class i.e returns a char from index where • setCharAt() method sets the character ch at the where index. • <<where>> should be >= 0 and should not specify a position beyond the end of the buffer StringBuffer strbuf = new StringBuffer("Object oriented"); System.out.println(strbuf); strbuf.setCharAt(10,'X'); System.out.println(strbuf); Object oriented Object oriXnted
Inserting charcters • Insert() method can be used for inserting characters • Insert() method is also overloaded • StringBuffer insert(int index , String str); • StringBuffer insert(int index , char ch); • StringBuffer insert(int index, Object obj); • << index >> must be within permitted range and should be positive
StringBuffer strbuf = new StringBuffer("Java"); strbuf.insert(3," "); System.out.println(strbuf); Jav a StringBuffer strbuf = new StringBuffer("Java"); strbuf.insert(4," "); System.out.println(strbuf); System.out.println(strbuf.length()); Java 5 StringBuffer strbuf = new StringBuffer("Java"); strbuf.insert(2,“ Programming "); System.out.println(strbuf); System.out.println(strbuf.length()); JaProgrammingva 15 StringBuffer strbuf = new StringBuffer("Java"); strbuf.insert(2,new circle(10)); System.out.println(strbuf); System.out.println(strbuf.length()); ?
Reverse the String Buffer • Use reverse() method • Syntax: StringBuffer reverse(); • Examples : StringBuffer s1 = new StringBuffer(“Object”); System.out.println(s1.reverse());
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. Deleting a Range of charcaters • To delete characters in range use delete() method • Syntax delete() : • StringBuffer delete(int startIndex , int endIndex) • << Deletes characters from startIndex to endIndex -1 >> • startIndex,endIndex should be positive and within permitted range and endIndex > startIndex
void setLength(int len)[Self Study] • Sets the length of the buffer within StringBuffer object • << len >> specifies the length of the buffer • << len >> should be non negative • If <<len>> is less than current length returned by length() then extra characters will be lost
StringBuffer strbuf = new StringBuffer(); System.out.println(strbuf.length()); System.out.println(strbuf.capacity()); strbuf.setLength(20); System.out.println(strbuf.length()); System.out.println(strbuf.capacity()); StringBuffer strbuf1 = new StringBuffer("Object oriented"); System.out.println(strbuf1.length()); System.out.println(strbuf1.capacity()); strbuf1.setLength(20); System.out.println(strbuf1.length()); System.out.println(strbuf1.capacity()); StringBuffer strbuf2 = new StringBuffer("Object oriented"); System.out.println(strbuf2.length()); System.out.println(strbuf2.capacity()); strbuf2.setLength(40); System.out.println(strbuf2.length()); System.out.println(strbuf2.capacity()); 0 16 20 34 15 31 20 31 15 31 40 64
StringBuffer str = new StringBuffer("Object Oriented Programming"); System.out.println(str.length()); System.out.println(str.capacity()); str.setLength(100); System.out.println(str.length()); System.out.println(str.capacity()); str.setLength(200); System.out.println(str.length()); System.out.println(str.capacity()); 27 43 100 100 200 202 If after insertion or appending the length execeeds capacity then new capacity will be ( 2 * oldcapacity + 2 )
Appending [Adding at the End] String Buffer • append() method can be used for adding at the end of StringBuffer. • Append() is overloaded with following foms: • StringBuffer append(String str) • StringBuffer append(int num) • StringBuffer append(Object obj) • First Method adds String str in the end • Second converts num into string and then adds in the end • Third method calls toString() on obj and then String form of obj will be inserted in the end. [ In this case obj must supply a suitable toString() method otherwise it will be taken from Object] Updates this parameter
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); strbuf.append(6.1); System.out.println(strbuf); Object oriented Object oriented6.1
Hashcode of Circle strbuf.append(6.1); System.out.println(strbuf); Circle c1 = new Circle(10.56); strbuf.append(c1); System.out.println(strbuf); } } Object oriented6.16.1 Object oriented6.16.1Circle@82ba41
class Circle { private double radius; Circle(double radius) { this.radius = radius; } public String toString() { return "Circle with Radius:"+radius; } } OUTPUT JavaCircle with Radius:10.56 class CircleTest { public static void main(String args[]) { StringBuffer strbuf = new StringBuffer("Java"); Circle c1 = new Circle(10.56); strbuf.append(c1); System.out.println(strbuf); } }