Java StringBuffer ClassJava StringBuffer class is used to create mutable (modifiable) String objects. The StringBuffer class in Java is the 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.Important Constructors of StringBuffer Class
Important methods of StringBuffer class
What is a mutable String?A String that can be modified or changed is known as mutable String. StringBuffer and StringBuilder classes are used for creating mutable strings. 1) StringBuffer Class append() MethodThe append() method concatenates the given argument with this String. StringBufferExample.java Output: Hello Java 2) StringBuffer insert() MethodThe insert() method inserts the given String with this string at the given position. StringBufferExample2.java Output: HJavaello 3) StringBuffer replace() MethodThe replace() method replaces the given String from the specified beginIndex and endIndex. StringBufferExample3.java Output: HJavalo 4) StringBuffer delete() MethodThe delete() method of the StringBuffer class deletes the String from the specified beginIndex to endIndex. StringBufferExample4.java Output: Hlo 5) StringBuffer reverse() MethodThe reverse() method of the StringBuilder class reverses the current String. StringBufferExample5.java Output: olleH 6) StringBuffer capacity() MethodThe capacity() method of the StringBuffer class returns the current capacity of the buffer. The default capacity of the buffer is 16. If the number of character increases from its current capacity, it increases the capacity by (oldcapacity*2)+2. For example if your current capacity is 16, it will be (16*2)+2=34. StringBufferExample6.java Output: 16 16 34 7) StringBuffer ensureCapacity() methodThe ensureCapacity() method of the StringBuffer class ensures that the given capacity is the minimum to the current capacity. If it is greater than the current capacity, it increases the capacity by (oldcapacity*2)+2. For example if your current capacity is 16, it will be (16*2)+2=34. StringBufferExample7.java Output: 16 16 34 34 70 Next TopicStringBuilder class |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India