String in JAVA 4
StringBuffer Class
Java StringBuffer class is used to created mutable (modifiable) string object. The StringBuffer class in java is same as String class except it is mutable i.e. it can be changed.
Note : Java StringBuffer class is thread-safe i.e. multiple threads cannot access it simultaneously. So it is safe and will result in an order.
StringBuffer Constructors
1. StringBuffer ( )
2. StringBuffer ( int size )
3. StringBuffer ( String str )
4. StringBuffer ( charSequence [ ]ch )
1) StringBuffer() creates an empty string buffer and reserves room for 16 characters.
2) StringBuffer(int size) creates an empty string and takes an integer argument to set capacity of the buffer.
3) StringBuffer(String str) creates a string buffer with the specified string
Example showing difference between String and StringBuffer:
class Test {
public static void main(String args[])
{
String str = "sky";
str.concat("nils");
System.out.println(str); // Output: sky
StringBuffer strB = new StringBuffer("sky");
strB.append("nils");
System.out.println(strB); // Output: skynils
}
}
Important methods of StringBuffer class
The following methods are some most commonly used methods of StringBuffer class.
1) append( )
This method will concatenate the string representation of any type of data to the end of the invoking StringBuffer object. append() method has several overloaded forms.
StringBuffer append(String str)
StringBuffer append(int n)
StringBuffer append(Object obj)
The string representation of each parameter is appended to StringBuffer object.
StringBuffer str = new StringBuffer("test");
str.append(123);
System.out.println(str);
Output : test123
2) insert( )
This method inserts one string into another. Here are few forms of insert() method.
StringBuffer insert(int index, String str)
StringBuffer insert(int index, int num)
StringBuffer insert(int index, Object obj)
Here the first parameter gives the index at which position the string will be inserted and string representation of second parameter is inserted into StringBuffer object.
StringBuffer str = new StringBuffer("123");
str.insert(1, "test");
System.out.println(str);
Output : 1test23
3) replace( )
This method replaces the string from specified startIndex to the endIndex.
StringBuffer str = new StringBuffer("Hello World");
str.replace( 6, 11, "java");
System.out.println(str);
Output : Hello java
4) delete( )
The method deletes the string from the specified startIndex to endIndex.
StringBuffer str=new StringBuffer("Hello");
str.delete(1,3);
System.out.println(str);
Output : Hlo
5) reverse( )
This method reverses the characters within a StringBuffer object.
StringBuffer str = new StringBuffer("Hello");
str.reverse();
System.out.println(str);
Output : olleH
6) capacity( )
This method returns the current capacity of StringBuffer object.
StringBuffer str = new StringBuffer();
System.out.println( str.capacity() );
Output : 16
7) ensureCapacity( )
This method is used to ensure minimum capacity of StringBuffer object.
StringBuffer str = new StringBuffer("hello");
str.ensureCapacity(10);
Difference between String and StringBuffer Class:
String Class | StringBuffer Class |
String class is immutable. | StringBuffer class is mutable. |
String is slow and consumes more memory when you concat too many strings because every time it creates new instance. | StringBuffer is fast and consumes less memory when you cancat strings. |
String class overrides the equals() method of Object class. So you can compare the contents of two strings by equals() method. | StringBuffer class doesn't override the equals() method of Object class. |
No comments:
Post a Comment
Hai , Post your comment . (required, Bugs, Errors )