JAVA-String builder - Notes By ShariqSP
Introduction to StringBuilder Class
The StringBuilder class in Java is similar to StringBuffer but is not synchronized, which makes it faster in performance. StringBuilder is used to create mutable sequences of characters.
Methods of the StringBuilder 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 StringBuilder object.
StringBuilder sb = new StringBuilder("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 StringBuilder object.
StringBuilder sb = new StringBuilder("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 StringBuilder object.
StringBuilder sb = new StringBuilder("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 StringBuilder object.
StringBuilder sb = new StringBuilder("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 StringBuilder object.
StringBuilder sb = new StringBuilder("Hello");
sb.replace(1, 4, "i");
System.out.println(sb); // Output: "Hilo"
6. reverse():
The reverse() method reverses the characters in the StringBuilder object.
StringBuilder sb = new StringBuilder("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 StringBuilder object.
StringBuilder sb = new StringBuilder("Hello");
char ch = sb.charAt(1);
System.out.println(ch); // Output: 'e'
8. length():
The length() method returns the length of the StringBuilder object.
StringBuilder sb = new StringBuilder("Hello");
int len = sb.length();
System.out.println(len); // Output: 5
9. substring(int start):
The substring() method returns a substring of the StringBuilder object, starting from the specified start index.
StringBuilder sb = new StringBuilder("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 StringBuilder object, starting from the specified start index and ending at the specified end index (exclusive).
StringBuilder sb = new StringBuilder("Hello World");
String substr = sb.substring(6, 11);
System.out.println(substr); // Output: "World"
Differences between String, StringBuffer, and StringBuilder
Aspect | String | StringBuffer | StringBuilder |
---|---|---|---|
Mutability | Immutable (Cannot be changed) | Mutable (Can be changed) | Mutable (Can be changed) |
Thread Safety | Thread-safe (Immutable) | Thread-safe (Synchronized) | Not thread-safe (Not synchronized) |
Performance | Comparatively slower | Slower due to synchronization | Faster due to lack of synchronization |
Use Cases | When immutability and thread safety are required | When multi-threading environment and synchronization are required | When performance is crucial and no thread safety is required |
Interview Questions on StringBuilder and StringBuffer Classes
Interview Questions:
- What are StringBuilder and StringBuffer classes in Java?
- Explain the differences between StringBuilder and StringBuffer.
- What is the purpose of using StringBuilder or StringBuffer over the String class?
- How do StringBuilder and StringBuffer achieve mutability compared to the immutability of the String class?
- When should you use StringBuilder and when should you use StringBuffer?
- Explain the performance differences between StringBuilder and StringBuffer.
- What are some important methods provided by the StringBuilder and StringBuffer classes?
- How does the append() method work in StringBuilder and StringBuffer?
- What is the significance of the capacity() and ensureCapacity() methods in StringBuilder and StringBuffer?
- Explain the reverse() method in StringBuilder and StringBuffer.
Multiple Choice Questions (MCQs):
- Which class is used for mutable string manipulation in Java?
a) String
b) StringBuilder
c) StringBuffer
d) Both b and c
Answer: d) Both b and c - Which class is synchronized and thread-safe?
a) String
b) StringBuilder
c) StringBuffer
d) Both a and b
Answer: c) StringBuffer - Which method is used to append characters to a StringBuilder or StringBuffer?
a) concat()
b) append()
c) add()
d) concatenate()
Answer: b) append() - Which method is used to reverse the content of a StringBuilder or StringBuffer?
a) reverse()
b) flip()
c) invert()
d) swap()
Answer: a) reverse() - Which class is preferred for single-threaded environments due to its better performance?
a) String
b) StringBuilder
c) StringBuffer
d) All have similar performance
Answer: b) StringBuilder