Which method of StringBuffer class is used to?
Show
As we know that there are basically two types of java objects, they are mutable and immutable. Immutable objects are those objects whose contents cannot be modified once created. Whenever the content of an immutable object is changed, there will be the creation of new objects. In the case of a mutable object, we can change the contents of an existing object which does not result in the creation of a new object. Therefore mutable strings are those strings whose content can be changed without creating new objects. StringBuffer and StringBuilder are mutable versions of String in java, whereas the java String class is immutable. Therefore StringBuffer is a mutable String used to provide mutability to String Objects. String class contains a fixed-length, immutable sequence of characters, whereas string buffer has an expandable length and writable sequence of characters. How to Use String Buffer Class in Java?Here are some points which show how we can use StringBuffer in java.
StringBuffer ConstructorsThe following are String buffer constructors:
Here is the declaration of StringBuffer Class:
Methods of StringBuffer Class in JavaNow we will see what different methods and fields available in StringBuffer are. Here is the list of commonly used methods available in the StringBuffer class:
Examples of StringBuffer Class in JavaHere are some of the examples of StringBuffer class which are given below: Example #1Let us see a basic example of the StringBuffer class. Code:
The above code shows the creation of java StringBuffer and its different methods. The following output will be produced. Output: Example #2In this example, we will see some more methods of the StringBuffer class. Code:
The above code will display the following as output. Output: In the above example, we have seen how to create a StringBuffer class and usage of its methods. ConclusionFrom the above discussion, we have a clear understanding of StringBuffer in java, how it is created, and the different methods available in the StringBuffer class. Also, StringBuffer is thread-safe; therefore, it can be used in a multithreading environment. Recommended ArticlesThis is a guide to StringBuffer Class in Java. Here we discuss the constructors, methods, and how to use the StringBuffer class in java, along with an example and code implementation. You can also go through our other suggested articles to learn more –
Which of this method of class StringBuffer is used to concatenate the String representation to the end of invoking String?Explanation: append() method of class StringBuffer is used to concatenate the string representation to the end of invoking string.
Which of these methods of class StringBuffer is used to extract a substring from a String object?The substring(int start) method of StringBuffer class is the inbuilt method used to return a substring start from index start and extends to end of this sequence.
Which of this method of class StringBuffer is used to reverse sequence of characters?In order to reverse the sequence of characters in a StringBuffer Object, we use the reverse() method.
Which method can be used to set the length of the buffer within a StringBuffer object?The setLength(int newLength) method is used to change the length of the StringBuffer Object. It sets the length of the character sequence. The character sequence is updated to a new one whose length is determined by the value passed as the parameter in the method.
|