• StringBuffer is a thread safe, mutable sequence of characters.
  • A StringBuffer is like a String , but can be modified in the same memory location.
By using following constructors we can create StringBuffer class object.

Constructors;

1.public StringBuffer(): 

  • It  creates empty StringBuffer object with default capacity 16 characters, it means it holds 16 empty locations.
  1. package com.instanceofjavaforus;
  2. public Class SBDemo{ 
  3.  
  4. public static void main (String args[]) {
  5.  
  6.   StringBuffer sb= new StringBuffer();
  7.   System.out.println(sb.capacity());
  8. }
  9. }
  10.  
  11. OutPut:
  12. 16


2.public StringBuffer(int capacity):

  • It creates StringBuffer object with given capacity.
  • Capacity value should be positive value. means >=0.
  • If we pass negative value JVM throws "java.lang.NegativeArraySizeException".

  1. package com.instanceofjavaforus;
  2. public Class SBDemo{ 
  3.  
  4. public static void main (String args[]) {
  5.  
  6.  StringBuffer sb= new StringBuffer(12);
  7.  System.out.println(sb.capacity());
  8.  
  9. }
  10. }

  11. OutPut:
  12. 12

3.public StringBuffer(String s):

  • It creates StringBuffer object characters available in passed String object and with default capacity 16 (16+String length) .
  1. package com.instanceofjavaforus;
  2. public Class SBDemo{ 
  3.  
  4. public static void main (String args[]) {
  5.  
  6.  StringBuffer sb= new StringBuffer(new String("abc"));
  7.  System.out.println(sb.capacity());
  8.  
  9. }
  10. }

  11. OutPut:
  12. 19

4.public StringBuffer(charSequence s):

  •  It creates StringBuffer object characters available in passed charSequence object and with default capacity 16 (16+charSequence length) .
  1. package com.instanceofjavaforus;
  2. public Class SBDemo{ 
  3.  
  4. public static void main (String args[]) {
  5.  
  6.  CharSequence cs = "abc";
  7.  StringBuffer sb= new StringBuffer(cs);
  8.  System.out.println(sb.capacity());
  9.  
  10. }
  11. }

  12. OutPut:
  13. 19

Methods :

1.public StringBuffer append(XXX s);

  • append(String s) methods concatenates the given String and returns updated StringBuffer object. 
  • There are 8 methods with same name for accepting 8 primitive data type values.
  • And 3 more methods for accepting StringBuffer object , Object and String objects.
  1. public StringBuffer append(boolean b)
  2. public StringBuffer append(char c) 
  3. public StringBuffer append(char[] str) 
  4. public StringBuffer append(char[] str, int offset, int len) 
  5. public StringBuffer append(double d) 
  6. public StringBuffer append(float f)
  7. public StringBuffer append(int i) 
  8. public StringBuffer append(long l) 
  9. public StringBuffer append(Object obj) 
  10. public StringBuffer append(StringBuffer sb) 
  11. public StringBuffer append(String str) 

  1. package com.instanceofjavaforus;
  2. public Class SBDemo{ 
  3.  
  4. public static void main (String args[]) {
  5.  
  6.   StringBuffer sb= new StringBuffer();
  7.   sb.append("abc");
  8.   System.out.println(sb);
  9. }
  10. }
  11.  
  12. OutPut:
  13. abc

 

2.public StringBuffer insert(XXX s);

  • where xxx is java data types 
  • overloaded to take all possible java data type values
  1. public StringBuffer insert(int offset, boolean b)
  2. public StringBuffer insert(int offset, char c)
  3. public insert(int offset, char[] str)
  4. public StringBuffer insert(int index, char[] str, int offset, int len)
  5. public StringBuffer insert(int offset, float f)  
  6. public StringBuffer insert(int offset, int i)
  7. public StringBuffer insert(int offset, long l) 
  8. public StringBuffer insert(int offset, Object obj) 
  9. public StringBuffer insert(int offset, String str)

  1. package com.instanceofjavaforus;
  2. public Class SBDemo{ 
  3.  
  4. public static void main (String args[]) {
  5.  
  6.   StringBuffer sb= new StringBuffer("abc");
  7.    sb.insert(1, "c");
  8.   System.out.println(sb);
  9. }
  10. }
  11.  
  12. OutPut:
  13. acbc

 

3. public StringBuffer deleteCharAt(int index):

  • This method used to delete character at specified index.
  1. package com.instanceofjavaforus;
  2. public Class SBDemo{ 
  3.  
  4. public static void main (String args[]) {
  5.  
  6.   StringBuffer sb= new StringBuffer("Java");
  7.    sb.deleteCharAt(1);
  8.   System.out.println(sb);
  9. }
  10. }
  11.  
  12. OutPut:
  13. Jva

 

4. public StringBuffer delete(int start , int end):

  • This method used to delete characters from specified start index to end index
  1. package com.instanceofjavaforus;
  2. public Class SBDemo{ 
  3.  
  4. public static void main (String args[]) {
  5.  
  6.   StringBuffer sb= new StringBuffer("Java Language");
  7.    sb.delete(1,3);
  8.   System.out.println(sb);
  9. }
  10. }
  11.  
  12. OutPut:
  13. Ja Language

 

5. public StringBuffer setCharAt(int index, char ch):

  • This method used to insert character at specified index
  1. package com.instanceofjavaforus;
  2. public Class SBDemo{ 
  3.  
  4. public static void main (String args[]) {
  5.  
  6.   StringBuffer sb= new StringBuffer("Java Language");
  7.       sb.insert(0, 'S');
  8.   System.out.println(sb);
  9. }
  10. }
  11.  
  12. OutPut:
  13. SJava Language



6. public int indexOf(String str):

  • This method returns index of specified sub string. if not found returns -1.
  1. package com.instanceofjavaforus;
  2. public Class SBDemo{ 
  3.  
  4. public static void main (String args[]) {
  5.  
  6.   StringBuffer sb= new StringBuffer("Java Language");
  7.    System.out.println(sb.indexOf("J"));
  8. }
  9. }
  10.  
  11. OutPut:
  12. 0

7. public int indexOf(String str, int index):

  • This method returns index of specified sub string. if not found returns -1.
  1. package com.instanceofjavaforus;
  2. public Class SBDemo{ 
  3.  
  4. public static void main (String args[]) {
  5.  
  6.   StringBuffer sb= new StringBuffer("Java Language");
  7.  System.out.println(sb.indexOf("a",4));
  8. }
  9. }
  10.  
  11. OutPut:
  12. 6

8. public int lastIndexOf(String str):

  • This method returns last occurrence of  of specified sub string index . if not found returns -1.
  1. package com.instanceofjavaforus;
  2. public Class SBDemo{ 
  3.  
  4. public static void main (String args[]) {
  5.  
  6.   StringBuffer sb= new StringBuffer("Java Language");
  7.  System.out.println(sb.lastIndexOf("a"));
  8. }
  9. }
  10.  
  11. OutPut:
  12. 10

9. public int length():

  • This method returns length of StringBuffer.
  1. package com.instanceofjavaforus;
  2. public Class SBDemo{ 
  3.  
  4. public static void main (String args[]) {
  5.  
  6.   StringBuffer sb= new StringBuffer("Java Language");
  7.  System.out.println(sb.length());
  8. }
  9.  
  10. OutPut:
  11. 13

10. public StringBuffer replace(int start, int end, String s):

  • This method replaces the characters from start index to end index with specified string.
  1. package com.instanceofjavaforus;
  2. public Class SBDemo{ 
  3.  
  4. public static void main (String args[]) {
  5.  
  6.   StringBuffer sb= new StringBuffer("Java Language");
  7.      System.out.println(sb.replace(0,4,"Instance of java"));
  8. }
  9.  
  10. OutPut:
  11. Instance of java Language

11. public StringBuffer reverse():

  • This method returns reverse of sequence of characters.
  1. package com.instanceofjavaforus;
  2. public Class SBDemo{ 
  3.  
  4. public static void main (String args[]) {
  5.  
  6.   StringBuffer sb= new StringBuffer("Java Language");
  7.        System.out.println(sb.reverse());
  8. }
  9.  
  10. OutPut:
  11. egaugnaL avaJ


12. public String subString(int start, int end):

  • This method returns sub string from start index to end index as a string.
  1. package com.instanceofjavaforus;
  2. public Class SBDemo{ 
  3.  
  4. public static void main (String args[]) {
  5.  
  6.   StringBuffer sb= new StringBuffer("Java Language");
  7.     System.out.println(sb.substring(1,3));
  8.  
  9. }
  10.  
  11. OutPut:
  12. av

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu