0% found this document useful (0 votes)
2 views

Java Imp Topics

The document explains the differences between compiled and interpreted languages, provides Java interview questions, and details the use of StringBuilder in Java. StringBuilder is a mutable sequence of characters that allows efficient string modifications without creating new objects, making it preferable for extensive string manipulations. Key methods of StringBuilder include append, insert, delete, and reverse, which modify the string in place.

Uploaded by

Ajit s Adin
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java Imp Topics

The document explains the differences between compiled and interpreted languages, provides Java interview questions, and details the use of StringBuilder in Java. StringBuilder is a mutable sequence of characters that allows efficient string modifications without creating new objects, making it preferable for extensive string manipulations. Key methods of StringBuilder include append, insert, delete, and reverse, which modify the string in place.

Uploaded by

Ajit s Adin
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

COMPILED VS INTERPRETED LANGUAGE

https://www.geeksforgeeks.org/difference-between-compiled-and-interpreted-language/

JAVA INTERVIWE QUESTIONS

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/

JAVA STRING BUILDER


Scaler academy:https://www.scaler.com/topics/java/stringbuilder-in-java/

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.

Key Points about StringBuilder

1. Mutability: Unlike String in Java, which is immutable (cannot be changed once


created), StringBuilder is mutable, meaning it allows you to modify the content of
the same object without creating new instances.
2. Efficiency: StringBuilder is generally faster and more memory-efficient than
String when building or modifying large strings repeatedly, as it does not create a
new string every time you change it.
3. No Synchronization: StringBuilder is not synchronized, meaning it is not thread-
safe. This makes it faster than StringBuffer (another mutable alternative) but
unsuitable for concurrent or multi-threaded environments where StringBuffer
would be preferable.

Example Usage of StringBuilder

Suppose we need to concatenate several words together in a loop:

java
Copy code
public class Example {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();

// Append words to the StringBuilder


sb.append("Hello");
sb.append(" ");

1
sb.append("world");

System.out.println(sb.toString()); // Output: "Hello world"


}
}

Why Use StringBuilder?

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.

When to Use 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.

In summary, StringBuilder provides a flexible and efficient way to manipulate strings in


Java, especially when repeated modifications are required.

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");

// Append additional strings


sb.append(" world");
sb.append("!");

System.out.println(sb.toString()); // Output: "Hello world!"


}
}

2. insert(int offset, String str)

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");

// Insert a string at index 6


sb.insert(6, "beautiful ");

System.out.println(sb.toString()); // Output: "Hello beautiful


world"
}
}

3. delete(int start, int end)

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");

// Delete characters from index 6 to 16


sb.delete(6, 16);

System.out.println(sb.toString()); // Output: "Hello world"


}

3
}

4. reverse()

The reverse method reverses the character sequence in the StringBuilder.

java
Copy code
public class StringBuilderExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello world");

// Reverse the character sequence


sb.reverse();

System.out.println(sb.toString()); // Output: "dlrow olleH"


}
}

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

1. After append: "Java programming"


2. After insert: "Java is fun programming"
3. After delete: "Java programming"
4. After reverse: "gnimmargorp avaJ"
4
Each method modifies the StringBuilder in place, making it efficient for sequential string
modifications.

4o

You might also like