Common interview questions related to String, StringBuffer, and StringBuilder in Java
1. String Questions
What is a String in Java?
A
String
is an immutable sequence of characters in Java, represented by thejava.lang.String
class.
Why is String immutable in Java?
For security, thread safety, caching (String Pool), and performance optimization.
What is the String Pool?
A special memory area in the heap where Java stores String literals to optimize memory usage.
Difference between
String s = "hello"
andString s = new String("hello")
?String s = "hello"
checks the String Pool first;new String("hello")
creates a new object in heap memory.
How to compare two Strings in Java?
Using
equals()
(content comparison) or==
(reference comparison).
Why should
char[]
be preferred over String for passwords?Strings are immutable and stay in memory longer, while
char[]
can be wiped after use.
What are the ways to create an immutable class in Java?
Make the class
final
, fieldsprivate
andfinal
, and avoid setters.
How does String concatenation work in Java?
Using
+
operator (internally usesStringBuilder
in loops) orconcat()
method.
2. StringBuffer Questions
What is StringBuffer?
A mutable, thread-safe (synchronized) class for string manipulation.
Why is StringBuffer thread-safe?
Its methods are synchronized.
When should you use StringBuffer instead of StringBuilder?
In multithreaded environments where thread safety is required.
Difference between String and StringBuffer?
String is immutable, StringBuffer is mutable and thread-safe.
How does StringBuffer improve performance over String?
Avoids creating multiple objects during modifications.
What is the default capacity of StringBuffer?
16 characters (if not specified).
How does StringBuffer increase its capacity?
New capacity =
(current_capacity + 1) * 2
.
3. StringBuilder Questions
What is StringBuilder?
A mutable, non-thread-safe alternative to StringBuffer for faster performance.
Why is StringBuilder faster than StringBuffer?
Because it is not synchronized.
When should you use StringBuilder?
In single-threaded environments where performance is critical.
Difference between StringBuffer and StringBuilder?
StringBuffer is thread-safe (synchronized), StringBuilder is not.
Which one is better for concatenation in a loop: String, StringBuffer, or StringBuilder?
StringBuilder
(due to mutability and no synchronization overhead).
Is StringBuilder more memory-efficient than String?
Yes, because it avoids creating intermediate String objects.
Can we convert StringBuilder to String?
Yes, using
toString()
method.
Comparison Questions
String vs StringBuffer vs StringBuilder?
String: Immutable, thread-safe (due to immutability).
StringBuffer: Mutable, thread-safe (synchronized).
StringBuilder: Mutable, not thread-safe, faster.
Which is the best choice among String, StringBuffer, and StringBuilder?
Depends on use case:
Use
String
for constants.Use
StringBuffer
for multithreaded scenarios.Use
StringBuilder
for single-threaded performance-critical cases.
How does Java handle String concatenation with
+
operator?Uses
StringBuilder
internally in loops (since Java 5).
Can we use StringBuffer or StringBuilder as keys in HashMap?
Not recommended because they are mutable, and hash codes may change.
Code-Based Questions
Predict the output:
String s1 = "Java"; String s2 = new String("Java"); System.out.println(s1 == s2); // false (different references)
What happens here?
String str = "Hello"; str.concat(" World"); System.out.println(str); // "Hello" (Strings are immutable)
Efficient way to reverse a String?
new StringBuilder(str).reverse().toString();
How many objects are created here?
String s1 = "A"; String s2 = "A"; String s3 = new String("A");
Answer: 2 (one in String Pool, one in heap via
new
).
Advanced Questions
How does
intern()
method work?Returns a canonical representation of the String from the String Pool.
Why is
String
popular as a HashMap key?Because it's immutable, ensuring hashcode consistency.
Impact of using
+
in a loop vsStringBuilder
?+
in a loop creates multiple intermediate objects, whileStringBuilder
is efficient.
Can we inherit String class?
No, because it's
final
.