It is a
thread-safe, mutable sequence of characters. A StringBuffer is like a string,
but can be modified in the same memory location. StringBuffer class objects are
mutable objects. Thus the sequence of character class can change at the runtime. Thus using object of the StringBuffer class, we can represent mutable
sequence of characters. In order to manipulate the data present inside the
object of StringBuffer class, StringBuffer supports some methods. None of the
methods belonging to the StringBuffer class creates a new StringBuffer class
object. Every method of the StringBuffer class would be changing the data
within the same object and returns the address of the same object.
In StringBuffer class we have 4
constructors –
1. public StringBuffer() – Creates
Empty StringBuffer with default capacity as 16 buffers, it means is has 16
empty locations
2. public StringBuffer(int capacity) – Creates
empty StringBuffer object with the given capacity. Capacity value should be
>=0, if we pass negative number, JVM throws “java.lang.NegativeArraySizeException”.
3. public StringBuffer(String s) – Creates
StringBuffer object with given string object data. It performs copy from string
to StringBuffer object. The default capacity is [16+s.length()]
4. public StringBuffer(charSequence cs) – Creates
new StringBuffer object with the given cs object characters. It also performs
dtring copy from cs object to StringBuffer object. Its capacity is
[16+cs.length()]
Example –
public class
String1 {
public static void
main(String[] args) {
StringBuffer
sb = new
StringBuffer();
System.out.println("sb capacity : "+sb.capacity());
StringBuffer
sb1
= new StringBuffer(5);
System.out.println("sb1 capacity : "+sb1.capacity());
StringBuffer
sb2
= new StringBuffer("hello");
System.out.println("sb2 capacity : "+sb2.capacity());
StringBuffer
sb3
= new StringBuffer("java");
StringBuffer
sb4
= new StringBuffer(sb3);
System.out.println("sb3 capacity : "+sb3.capacity());
System.out.println("sb4 capacity : "+sb4.capacity());
}
}
Output –
sb capacity : 16
sb1 capacity : 5
sb2 capacity : 21
sb3 capacity : 20
sb4 capacity : 20
StringBuilder – It is
same as StringBuffer except that it is non-synchronized. It is available since
java 1.5 onwards. And its constructors are also same as StringBuffer.
Special operations that we can perform only
on StringBuffer and StringBuilder classes –
1.
Append
2.
Insert
3.
Delete
4.
Reverse
5. Replacing
character at given index.
Example –
public class
String1 {
public static void
main(String[] args) {
//append
StringBuilder
sb = new
StringBuilder("abc");
System.out.println(sb);
StringBuilder
sb1
= sb.append("d");
System.out.println(sb);
System.out.println(sb1);
System.out.println(sb==sb1);
System.out.println();
//insert
StringBuilder
sb2
= new StringBuilder("27101991");
System.out.println(sb2);
sb2.insert(2,'/');
sb2.insert(5,
'/');
System.out.println(sb2);
System.out.println();
//delete
StringBuilder
sb3
= new StringBuilder("coding
makes perfect");
sb3.delete(sb3.indexOf("makes"), sb3.indexOf("makes")+6); //
sb3.delete(6,4+4)
System.out.println(sb3);
System.out.println();
//reverse
StringBuilder
sb4
= new StringBuilder("hacker");
sb4.reverse();
System.out.println(sb4);
System.out.println();
//capacity and length
StringBuilder
sb5
= new StringBuilder();
System.out.println(sb5.capacity());
System.out.println(sb5.length());
sb5.insert(0,
"abc");
System.out.println(sb5.capacity());
System.out.println(sb5.length());
StringBuilder
sb6
= new StringBuilder("xyz");
sb6.insert(0,
"abc");
System.out.println(sb6.capacity());
System.out.println(sb6.length());
}
}
Output –
abc
abcd
abcd
true
27101991
27/10/1991
coding perfect
rekcah
16
0
16
3
19
6
Difference between String and StringBuffer
and StringBuilder
·
String is a immutable sequence of characters,
Whereas using object of StringBuilder class we can represent the mutable
sequence of characters but not in thread safe, Whereas StringBuffer is a thread
safe mutable sequence of characters.
·
String objects can be created in two ways – by assigning
string literal like String s1=”a”; and by using its available constructors,
Whereas StringBuilder/StringBuffer can only be created using its available
constructors.
·
String does not have default capacity, Whereas
StringBuffer / StringBuilder has default capacity as 16 buffers. It increases
its capacity automatically when size reaches maximum using its capacity
formulas (current capacity*2)+2
·
Using string object we can concat new string to
the new string to the current string in two ways – by using + and concat() method, Whereas using StringBuffer/StringBuilder we can
concat only using append() method.
·
Special operations like append, insert, delete,
reverse are only applicable with StringBuffer/StringBuilder, Whereas string is
immutable so we cannot perform this operation on string.
No comments:
Post a Comment