1 / 19

StringBuffer Class In Java

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

aderes
Download Presentation

StringBuffer Class In Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. StringBuffer Class In Java

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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()); ?

  11. Reverse the String Buffer • Use reverse() method • Syntax: StringBuffer reverse(); • Examples : StringBuffer s1 = new StringBuffer(“Object”); System.out.println(s1.reverse());

  12. 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

  13. 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

  14. 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

  15. 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 )

  16. 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

  17. 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

  18. 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

  19. 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); } }

More Related