Java Imp Topics
Java Imp Topics
https://www.geeksforgeeks.org/difference-between-compiled-and-interpreted-language/
https://www.mygreatlearning.com/blog/java-interview-questions/
https://www.geeksforgeeks.org/core-java-interview-questions-for-freshers/
https://www.geeksforgeeks.org/java-interview-questions/
StringBuilder in Java is a mutable sequence of characters that allows you to modify the
content of a string without creating a new object each time, which is more efficient when
performing numerous modifications, like appending, deleting, or inserting characters.
java
Copy code
public class Example {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
1
sb.append("world");
In cases where you need to modify a string frequently (e.g., appending in a loop),
StringBuilder is a better choice than String:
1. String (Immutable):
o When modifying a String, each modification creates a new String object.
For example, with concatenation str = str + "new", a new String is
created each time, which can be inefficient in terms of memory and
processing.
2. StringBuilder (Mutable):
o Using StringBuilder, you can directly modify the same object without
creating new instances. This results in better performance, especially when
making multiple modifications.
Methods in StringBuilder
append(String str): Adds the specified string to the end of the current
StringBuilder.
insert(int offset, String str): Inserts a string at the specified position.
delete(int start, int end): Removes characters from the specified start index to
the end index.
reverse(): Reverses the character sequence in the StringBuilder.
Use StringBuilder when you are performing extensive string manipulations (e.g., in
loops).
It is suitable for single-threaded environments due to its non-synchronized nature.
2
Here are examples demonstrating each of the commonly used methods in StringBuilder:
1. append(String str)
The append method adds a string to the end of the current StringBuilder object.
java
Copy code
public class StringBuilderExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello");
The insert method inserts a string at the specified index (offset) in the current
StringBuilder.
java
Copy code
public class StringBuilderExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello world");
The delete method removes characters from the specified start index to the end index
(excluding the end index).
java
Copy code
public class StringBuilderExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello beautiful world");
3
}
4. reverse()
java
Copy code
public class StringBuilderExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello world");
Combined Example
Here's a combined example using all of the above methods in one program:
java
Copy code
public class StringBuilderExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Java");
// Append
sb.append(" programming");
System.out.println("After append: " + sb); // Output: "Java
programming"
// Insert
sb.insert(5, "is fun ");
System.out.println("After insert: " + sb); // Output: "Java is fun
programming"
// Delete
sb.delete(5, 12);
System.out.println("After delete: " + sb); // Output: "Java
programming"
// Reverse
sb.reverse();
System.out.println("After reverse: " + sb); // Output:
"gnimmargorp avaJ"
}
}
Explanation of Output
4o