JAVA-String buffer - Notes By ShariqSP
Introduction to StringBuffer Class
The StringBuffer class in Java is used to create mutable sequences of characters. Unlike String, StringBuffer objects can be modified after they are created. StringBuffer provides various methods for string manipulation.
Methods of the StringBuffer Class:
- append(String s)
- insert(int offset, String s)
- delete(int start, int end)
- deleteCharAt(int index)
- replace(int start, int end, String str)
- reverse()
- charAt(int index)
- length()
- substring(int start)
- substring(int start, int end)
Examples:
1. append(String s):
The append() method appends the specified string to the end of the StringBuffer object.
StringBuffer sb = new StringBuffer("Hello");
sb.append(" World");
System.out.println(sb); // Output: "Hello World"
2. insert(int offset, String s):
The insert() method inserts the specified string at the specified position in the StringBuffer object.
StringBuffer sb = new StringBuffer("Hello");
sb.insert(5, " World");
System.out.println(sb); // Output: "Hello World"
3. delete(int start, int end):
The delete() method removes the characters from the specified start index to the specified end index (exclusive) from the StringBuffer object.
StringBuffer sb = new StringBuffer("Hello World");
sb.delete(5, 11);
System.out.println(sb); // Output: "Hello"
4. deleteCharAt(int index):
The deleteCharAt() method removes the character at the specified index from the StringBuffer object.
StringBuffer sb = new StringBuffer("Hello");
sb.deleteCharAt(1);
System.out.println(sb); // Output: "Hllo"
5. replace(int start, int end, String str):
The replace() method replaces the characters from the specified start index to the specified end index (exclusive) with the specified string in the StringBuffer object.
StringBuffer sb = new StringBuffer("Hello");
sb.replace(1, 4, "i");
System.out.println(sb); // Output: "Hilo"
6. reverse():
The reverse() method reverses the characters in the StringBuffer object.
StringBuffer sb = new StringBuffer("Hello");
sb.reverse();
System.out.println(sb); // Output: "olleH"
7. charAt(int index):
The charAt() method returns the character at the specified index position in the StringBuffer object.
StringBuffer sb = new StringBuffer("Hello");
char ch = sb.charAt(1);
System.out.println(ch); // Output: 'e'
8. length():
The length() method returns the length of the StringBuffer object.
StringBuffer sb = new StringBuffer("Hello");
int len = sb.length();
System.out.println(len); // Output: 5
9. substring(int start):
The substring() method returns a substring of the StringBuffer object, starting from the specified start index.
StringBuffer sb = new StringBuffer("Hello World");
String substr = sb.substring(6);
System.out.println(substr); // Output: "World"
10. substring(int start, int end):
The substring() method returns a substring of the StringBuffer object, starting from the specified start index and ending at the specified end index (exclusive).
StringBuffer sb = new StringBuffer("Hello World");
String substr = sb.substring(6, 11);
System.out.println(substr); // Output: "World"
Interview Questions on StringBuffer Class
Interview Questions:
- What is the StringBuffer class in Java?
- Explain the difference between the StringBuffer and String classes.
- Why is StringBuffer considered mutable in Java?
- What are some important methods provided by the StringBuffer class?
- Explain the significance of the append() method in the StringBuffer class.
- How does the capacity of a StringBuffer object change dynamically?
- What is the purpose of the setCharAt() method in the StringBuffer class?
- Explain the reverse() method in the StringBuffer class.
- What is the significance of the delete() and deleteCharAt() methods in the StringBuffer class?
- How does the StringBuffer class handle concurrency?
Multiple Choice Questions (MCQs):
- Which of the following classes is mutable in Java?
a) String
b) StringBuilder
c) StringBuffer
d) Both String and StringBuilder
Answer: c) StringBuffer - Which method is used to concatenate two strings in the StringBuffer class?
a) concat()
b) join()
c) append()
d) concatenate()
Answer: c) append() - What is the initial capacity of a StringBuffer object?
a) 10
b) 16
c) 20
d) It varies based on the input
Answer: b) 16 - Which method is used to obtain the length of a StringBuffer object?
a) size()
b) length()
c) getSize()
d) capacity()
Answer: b) length() - Which method is used to clear the contents of a StringBuffer object?
a) clear()
b) removeAll()
c) erase()
d) setLength(0)
Answer: d) setLength(0)