StringBuffer class in java
java programming and c, c++, Matlab, Python, HTML, CSS programming language, and new techniques news and any history.
java programming and c, c++, Matlab, Python, HTML, CSS programming language, and new techniques news and any history.
StringBuffer class
StringBuffer class is used to create a mutable string object. It represents growable and writable character sequence. As we know that String objects are immutable, so if we do a lot of changes with String objects, we will end up with a lot of memory leak.
So StringBuffer class is used when we have to make a lot of modifications to our string. It is also threaded safe i.e multiple threads cannot access it simultaneously. StringBuffer defines 4 constructors. They are,
- StringBuffer ( )
- StringBuffer ( int size )
- StringBuffer ( String str )
- StringBuffer ( charSequence [ ]ch )
StringBuffer()
creates an empty string buffer and reserves room for 16 characters.stringBuffer(int size)
creates an empty string and takes an integer argument to set capacity of the buffer.
Example showing a difference between String and StringBuffer
class Test { public static void main(String args[]) { String str = "study"; str.concat("tonight"); System.out.println(str); // Output: study StringBuffer strB = new StringBuffer("study"); strB.append("tonight"); System.out.println(strB); // Output: studytonight } }
Important methods of StringBuffer class
The following methods are some most commonly used methods of StringBuffer class.
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
insert()
This method inserts one string into another. Here are few forms of an 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 the string representation of the second parameter is inserted into StringBuffer object.
StringBuffer str = new StringBuffer("test"); str.insert(4, 123); System.out.println(str);
Output : test123
reverse()
This method reverses the characters within a StringBuffer object.
StringBuffer str = new StringBuffer("Hello"); str.reverse(); System.out.println(str);
Output : olleH
replace()
This method replaces the string from a specified start index to the end index.
StringBuffer str = new StringBuffer("Hello World"); str.replace( 6, 11, "java"); System.out.println(str);
Output : Hello java
capacity()
This method returns the current capacity of the StringBuffer object.
StringBuffer str = new StringBuffer(); System.out.println( str.capacity() );
Output : 16
ensureCapacity()
This method is used to ensure the minimum capacity of the StringBuffer object.
StringBuffer str = new StringBuffer("hello"); str.ensureCapacity(10);
Comments
Post a Comment