Answer:
- A
Stringobject can be created in two primary ways:- Using string literals:
String str = "Hello";
- Stored in the String pool, a special memory area in the JVM for strings, for memory optimization.
- Using the
newkeyword:String str = new String("Hello");
- Creates a new
Stringobject in the heap memory, even if the same string exists in the pool. - Avoid unless explicitly needed to create a distinct object.
- Creates a new
- Using string literals:
Answer:
CharSequenceis an interface representing a sequence of characters, whileStringis a concrete implementation ofCharSequence.- Key differences:
CharSequenceis a general-purpose API for various string-like objects (e.g.,String,StringBuilder,StringBuffer).Stringis immutable and provides additional methods for manipulating and comparing strings.
Answer:
- The
intern()method ensures that a string is stored in the String pool. - If the string already exists in the pool, it returns the reference; otherwise, it adds the string to the pool.
- This helps in saving memory by avoiding duplicate string objects.
Example:
String s1 = new String("Java").intern();
String s2 = "Java";
System.out.println(s1 == s2); // trueAnswer:
- The
split()method splits a string into an array of substrings using a specified delimiter (regular expression).
Example:
String str = "apple,banana,grape";
String[] fruits = str.split(",");
for (String fruit : fruits) {
System.out.println(fruit);
}Answer:
- Reversing a string manually involves iterating over its characters in reverse order and building the reversed string.
Example:
public class ReverseString {
public static String reverse(String str) {
StringBuilder reversed = new StringBuilder();
for (int i = str.length() - 1; i >= 0; i--) {
reversed.append(str.charAt(i));
}
return reversed.toString();
}
}Answer:
- A palindrome reads the same backward as forward. This can be checked by comparing characters from the beginning and end of the string.
Example:
public class PalindromeChecker {
public static boolean isPalindrome(String str) {
int left = 0, right = str.length() - 1;
while (left < right) {
if (str.charAt(left) != str.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
}Answer:
String.format()allows for dynamic construction of formatted strings using placeholders like%s(string),%d(integer),%f(float), etc.
Example:
String name = "John";
int score = 85;
String formatted = String.format("Hello %s, you scored %d marks!", name, score);
System.out.println(formatted);Answer:
- The
join()method concatenates multiple strings with a specified delimiter in between.
Example:
String result = String.join(", ", "apple", "banana", "grape");
System.out.println(result); // Output: apple, banana, grapeAnswer:
- Escape sequences are special characters prefixed with a backslash (
\) that represent certain actions or characters.- Examples:
\n: Newline\t: Tab\\: Backslash\": Double quote
- Examples:
Example:
String str = "Hello\nWorld";
System.out.println(str);Output:
Hello
World
Answer:
-
+operator:- Creates new string objects for each concatenation, leading to higher memory usage.
- Inefficient for repetitive concatenation in loops.
-
StringBuilder:- Uses a mutable buffer, reducing memory overhead.
- Best choice for multiple or repetitive string manipulations.
Example:
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");
System.out.println(sb.toString());