String, StringBuffer, and StringBuilder
Differentiate immutable String from mutable StringBuffer and StringBuilder. Learn performance, memory allocation, and thread safety.
Downloaded from PrepIQ (https://prepiq.online)
Q1. What is String immutability in Java?
String immutability means once a String object is created, it cannot be changed. If you modify a string, a new String object is created instead of modifying the existing one.
| Concept | Explanation |
|---|---|
| Immutable | Cannot be changed after creation |
| String Pool | Special memory region for String literals |
| Thread-Safe | No synchronization needed for strings |
| Performance | Can be cached and optimized |
String s1 = "Hello";
String s2 = s1;
String s3 = s1.concat(" World");
System.out.println(s1); // Hello
System.out.println(s3); // Hello World
Q2. What is the difference between String, StringBuffer, and StringBuilder?
| Feature | String | StringBuffer | StringBuilder |
|---|---|---|---|
| Mutability | Immutable | Mutable | Mutable |
| Thread-Safe | Yes (immutable) | Yes (synchronized) | No (not synchronized) |
| Performance | Slow (new objects) | Moderate | Fast (no sync overhead) |
| Use Case | Single concatenation | Multi-threading | Single-thread performance |
String str = "Hello";
str = str + " World";
StringBuffer sbf = new StringBuffer("Hello");
sbf.append(" World");
StringBuilder sbd = new StringBuilder("Hello");
sbd.append(" World");
Q3. What is String Pool (String Constant Pool) in Java?
String Pool is a special memory region where String literals are stored. Java checks if a String already exists in the pool before creating a new one.
String s1 = "Hello";
String s2 = "Hello";
String s3 = new String("Hello");
System.out.println(s1 == s2); // true
System.out.println(s1 == s3); // false
String s4 = s3.intern();
System.out.println(s1 == s4); // true
Q4. How do you compare Strings in Java? Explain ==, equals(), and compareTo().
String s1 = "Hello";
String s2 = "Hello";
String s3 = new String("Hello");
System.out.println(s1 == s2); // true
System.out.println(s1 == s3); // false
System.out.println(s1.equals(s3)); // true
System.out.println("Hello".equalsIgnoreCase("hello")); // true
System.out.println("Apple".compareTo("Banana")); // negative
Q5. What are important String methods in Java?
Java String class provides many useful methods for string manipulation, searching, and transformation.
String str = "Hello World";
str.length();
str.charAt(0);
str.substring(0, 5);
str.toUpperCase();
str.toLowerCase();
str.indexOf("o");
str.lastIndexOf("o");
str.contains("World");
str.replace("Hello", "Hi");
str.trim();
str.split(" ");
String.join("-", "A", "B", "C");
Q6. What are different ways to concatenate strings in Java?
String result = "Hello" + " " + "World";
String s1 = "Hello".concat(" World");
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 5; i++) sb.append("Number ").append(i);
StringBuffer sbf = new StringBuffer("Hello");
sbf.append(" World");
String joined = String.join(", ", "Apple", "Banana", "Cherry");
String formatted = String.format("Name: %s, Age: %d", "John", 30);
Q7. What are important StringBuffer methods?
StringBuffer sb = new StringBuffer("Hello");
sb.append(" World");
sb.insert(5, " World");
sb.delete(6, 11);
sb.replace(0, 5, "Hi");
sb.reverse();
sb.setCharAt(0, 'J');
sb.length();
sb.capacity();
sb.toString();
Q8. Why is StringBuilder faster than StringBuffer?
StringBuilder is faster because it does not use synchronization. StringBuffer methods are synchronized for thread-safety, which adds overhead.
StringBuffer sbf = new StringBuffer();
for (int i = 0; i < 100000; i++) sbf.append('a');
StringBuilder sbd = new StringBuilder();
for (int i = 0; i < 100000; i++) sbd.append('a');
Q9. What is String interning and the intern() method?
String s1 = "Hello";
String s2 = "Hello";
System.out.println(s1 == s2); // true
String s3 = new String("Hello");
String s4 = s3.intern();
System.out.println(s1 == s4); // trueString literals are automatically interned. The intern() method returns the canonical pooled representation.
String, StringBuffer, and StringBuilder
Differentiate immutable String from mutable StringBuffer and StringBuilder. Learn performance, memory allocation, and thread safety.
What is String immutability in Java?
String immutability means once a String object is created, it cannot be changed. If you modify a string, a new...
What is the difference between String, StringBuffer, and StringBuilder?
FeatureStringStringBufferStringBuilderMutabilityImmutableMutableMutableThread-SafeYes (immutable)Yes (synchron...
What is String Pool (String Constant Pool) in Java?
String Pool is a special memory region where String literals are stored. Java checks if a String already exist...
How do you compare Strings in Java? Explain ==, equals(), and compareTo().
String s1 = "Hello"; String s2 = "Hello"; String s3 = new String("Hello"); System.out.println(s1 == s2);...
What are important String methods in Java?
Java String class provides many useful methods for string manipulation, searching, and transformation.String s...
What are different ways to concatenate strings in Java?
String result = "Hello" + " " + "World"; String s1 = "Hello".concat(" World"); StringBuilder sb = new StringBu...
What are important StringBuffer methods?
StringBuffer sb = new StringBuffer("Hello"); sb.append(" World"); sb.insert(5, " World"); sb.delete(6, 11); sb...
Why is StringBuilder faster than StringBuffer?
StringBuilder is faster because it does not use synchronization. StringBuffer methods are synchronized for thr...
What is String interning and the intern() method?
String s1 = "Hello"; String s2 = "Hello"; System.out.println(s1 == s2); // true String s3 = new String("Hello...