Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
Strings in Java are a sequence of immutable characters. StringBuffer, on the other hand, is used to create a sequence of mutable characters. In this article we would deep dive into the concept of StringBuffer In Java. Following pointers will be discussed in this session,
So let us started, however, it is important we start with some constructors,
An empty string buffer with an initial capacity of 16 characters is created.
StringBuffer str=new StringBuffer();
The string buffer created is of the size specified in the argument.
StringBuffer str=new StringBuffer(20);
The argument specified sets the initial contents of the StringBuffer object and reserves space for 16 more characters without reallocation.
StringBuffer str=new StringBuffer(“Welcome”);
Let us continue with StringBuffer in Java article,
The methods used in StringBuffer are specified as follows:
It specifies the number of elements present.
import java.io.*; class Main { public static void main(String[] args) { StringBuffer str = new StringBuffer("JohnDoe"); int q = str.length(); System.out.println("Length : " + q); } }
Output:
Length : 7
The capacity of the StringBuffer can be found using this method.
class Main { public static void main(String[] args) { StringBuffer str = new StringBuffer("JohnDoe"); int q = str.capacity(); System.out.println("Capacity :" + q); } }
Output:
Capacity : 23
StringBuffer In Java: append():
The method is used add elements at the end of the StringBuffer.
import java.io.*; class Main { public static void main(String[] args) { StringBuffer str = new StringBuffer("John"); str.append("Doe"); System.out.println(str); // appends a string str.append(5); System.out.println(str); // appends a number } }
Output:
JohnDoe
JohnDoe5
The method is used to insert an element at the specified index position.
import java.io.*; class Main { public static void main(String[] args) { StringBuffer str = new StringBuffer("RockRoll"); str.insert(4, "and"); System.out.println(str); str.insert(0, 5); System.out.println(str); str.insert(5, 69.70d); System.out.println(str); str.insert(6, 69.42f); System.out.println(str); char arr[] = { 'h', 's', 'w', 'p', 'a' }; str.insert(2, arr); System.out.println(str); } }
Output:
RockandRoll
5RockandRoll
5Rock69.7andRoll
5Rock669.429.7andRoll
5Rhswpaock669.429.7andRoll
The method is used to reverse the elements present in the StringBuffer.
import java.io.*; class Main { public static void main(String[] args) { StringBuffer str = new StringBuffer("RockandRoll"); str.reverse(); System.out.println(str); } }
Output:
lloRdnakcoR
The method is used to delete the elements present in the StringBuffer. The first character to be removed is specified by the first index. The elements between the startIndex and endIndex-1 are deleted.
import java.io.*; class Main { public static void main(String[] args) { StringBuffer str = new StringBuffer("RockAndRoll"); str.delete(0, 4); System.out.println(str); } }
Output:
AndRoll
The method removes a single character within the string present in the StringBuffer. The int index specifies the location of the character.
import java.io.*; class Main { public static void main(String[] args) { StringBuffer str = new StringBuffer("RockAndRoll"); str.deleteCharAt(5); System.out.println(str); } }
Output:
RockAdRoll
The method is used to replace a set of elements or characters with another, inside the StringBuffer. The arguments startIndex and endIndex are present in this method. The substring from the startIndex till the endIndex -1 is replaced.
import java.io.*; class Main { public static void main(String[] args) { StringBuffer str = new StringBuffer("RockAndRoll"); str.replace(4, 7, "nor"); System.out.println(str); } }
Output:
RocknorRoll
ensureCapacity()
The capacity of the StringBuffer can be increased by this method. The new capacity is either the value specified by the user, or twice the current capacity plus 2 , depending on the size.
Example: If 16 is the current capacity: (16*2)+2.
class Main{ public static void main(String args[]){ StringBuffer str=new StringBuffer(); System.out.println(str.capacity()); //initial capacity str.append("Rock"); System.out.println(str.capacity()); //now 16 str.append("My name is John Doe"); System.out.println(str.capacity()); //(oldcapacity*2)+2 str.ensureCapacity(10); //no change System.out.println(str.capacity()); str.ensureCapacity(50); //now (34*2)+2 System.out.println(str.capacity()); //now 70 } }
Output:
16
16
34
34
70
StringBuffer appendCodePoint(int codePoint)
In this method the string representation of the codePoint is added to the characters present in StringBuffer.
import java.lang.*; public class Main { public static void main(String[] args) { StringBuffer str = new StringBuffer("RockAndRoll"); System.out.println("StringBuffer : " + str); //Appending the CodePoint as a string str.appendCodePoint(70); System.out.println("StringBuffer with codePoint : " + str); } }
Output:
StringBuffer : RockAndRoll
StringBuffer with codePoint : RockAndRollF
In this method the “Unicodenumber” of the character is returned at the index. The value of the index must be between 0 and length -1.
class Main { public static void main(String[] args) { //creating a StringBuffer StringBuffer s = new StringBuffer(); s.append("RockAndRoll"); //Getting the Unicode of character at position 7 int unicode = s.codePointAt(7); //Displaying the result System.out.println("Unicode of Character at index 7 : " + unicode); } }
Output:
Unicode of Character at index 7 : 82
This inbuilt method outputs a string representing the data present in the StringBuffer. A new String object is declared and initialized to get the character sequence from the StringBuffer object. The string sis then returned by toString().
import java.lang.*; class Main { public static void main(String[] args) { StringBuffer s = new StringBuffer("RockAndRoll"); System.out.println("String : " + s.toString()); } }
Output:
String : RockAndRoll
The trimToSize() is an inbuilt method. The capacity of the character sequence is trimmed is trimmed by using this method.
import java.lang.*; class Main { public static void main(String[] args) { StringBuffer s = new StringBuffer("RockAndRoll"); //adding another element s.append("Pop"); // isplaying initial capacity System.out.println("Capacity before trimming : " + s.capacity()); //Trimming s.trimToSize(); //Displaying the string System.out.println("String = " + s.toString()); //Displaying trimmed capacity System.out.println("Capacity after trimming : " + s.capacity()); } }
Output:
Capacity before trimming : 27
String : RockAndRollPop
Capacity after trimming : 14
There are various methods mentioned in this article that can be used accordingly with the StringBuffer class in java. These methods are efficient and the allow the user to modify the strings easily.
Thus we have come to an end of this article on ‘StringBuffer in Java’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this article and we will get back to you as soon as possible.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 7th December,2024 7th December SAT&SUN (Weekend Batch) | View Details |
edureka.co