Tuesday, 1 July 2014

StringBuffer (String str)

public StringBuffer (String str)
Constructs a string buffer initialized to the contents of the specified string. The initial capacity of the string buffer is 16 plus the length of the string argument.

class StringBufferConstructor4{
 public static void main(String args[]){
  StringBuffer str;
  String s1 = "efghij";
  str = new StringBuffer(s1);
  
  System.out.println("str = " + str);
  System.out.println("Length Of str is " + str.length());
  System.out.println("Initial Capacity is " + str.capacity());
 }
}

Output
str = efghij
Length Of str is 6
Initial Capacity is 22





Prevoius                                                 Next                                                 Home

StringBuffer (int capacity)

public StringBuffer(int capacity)
Constructs a string buffer with no characters in it and the specified initial capacity.

class StringBufferConstructor3{
 public static void main(String args[]){
  StringBuffer str;
  str = new StringBuffer(10);
  
  System.out.println("str = " + str);
  System.out.println("Length Of str is " + str.length());
  System.out.println("Initial Capacity is " + str.capacity());
 }
}

Output
str =
Length Of str is 0
Initial Capacity is 10




Prevoius                                                 Next                                                 Home

StringBuffer (CharSequence seq)

public StringBuffer(CharSequence seq)
Constructs a string buffer that contains the same characters as the specified CharSequence. The initial capacity of the string buffer is 16 plus the length of the CharSequence argument.

class StringBufferConstructor2{
 public static void main(String args[]){
  StringBuffer str;
  str = new StringBuffer("efghij");
  
  System.out.println("str = " + str);
  System.out.println("Length Of str is " + str.length());
  System.out.println("Initial Capacity is " + str.capacity());
 }
}

Output
str = efghij
Length Of str is 6
Initial Capacity is 22




Prevoius                                                 Next                                                 Home

StringBuffer ()

public StringBuffer()
Constructs a string buffer with initial capacity of 16characters in it. Initially there are no characters in it.

class StringBufferConstructor1{
 public static void main(String args[]){
  StringBuffer str = new StringBuffer();
  
  System.out.println("str = " + str);
  System.out.println("Initial Capacity is " + str.capacity());
 }
}

Output
str =
Initial Capacity is 16




Prevoius                                                 Next                                                 Home

Constructors

StringBuffer class provides below construtors.

StringBuffer()
StringBuffer(CharSequence seq)
StringBuffer(int capacity)
StringBuffer(String str)




Prevoius                                                 Next                                                 Home

String Vs StringBuffer

1. String is immutable, i.e, the contents of the string are not changed after creation. Where as StringBuffer is mutable.

2. Performance wise, StringBuffer is faster when performing concatenations. This is because when you concatenate a String, you are creating a new Stirng object every time since String is immutable. So StringBuffer is preferrable while performing many concatenations.

Example
class StringVsStringBuffer{

 static void concatStrings(){
  String s = " ";
  String s1 = "a";
  for(int i=0; i < 100000; i++){
   s = s.concat(s1);
  }
 }
 
 static void concatStringBuffer(){
  StringBuffer s = new StringBuffer("");
  String s1 = "a";
  for(int i=0; i < 100000; i++){
   s.append(s1);
  }
 }
 
 public static void main(String args[]){
  long time1 = System.currentTimeMillis();
  concatStrings();
  long time2 = System.currentTimeMillis();
  concatStringBuffer();
  long time3 = System.currentTimeMillis();
  
  System.out.print("Time taken for String append is ");
  System.out.println((time2-time1) + " Milliseconds");
  System.out.print("Time taken for StringBuffer append is ");
  System.out.println((time3-time2) + " Milliseconds");
 }
}

Output
Time taken for String append is 2377 Milliseconds
Time taken for StringBuffer append is 4 Milliseconds





Prevoius                                                 Next                                                 Home

StringBuffer

StringBuffer is like a string, but it is mutable. I.e, it can be modified. StringBuffer is final, so it can't be extended.

String Buffer is thread safe. The necessary methods in StringBuffer are synchronized.

Every string buffer has a capacity. Whenever the characters in string reaches the capacity, the capacity automatically made larger.





Prevoius                                                 Next                                                 Home