🔹 1. What is a String in Java?
-
In Java, String is a class (from
java.lang
) and not a primitive type. -
Strings are immutable, meaning once created, the value cannot be changed.
-
Any operation like concatenation, substring, replace, etc. always creates a new String object.
🔹 2. String Literals
-
A string literal is any sequence of characters written in double quotes
" "
.
-
When you write
"Java"
, Java checks in the String Pool (inside the heap). -
If
"Java"
already exists, the reference is reused. -
If not, a new object is created in the pool.
🔹 3. String Pool (a.k.a. Intern Pool)
-
A special area in the heap memory where string literals are stored.
-
Purpose → save memory by avoiding multiple copies of the same string.
🔹 4. new String() vs String Literal
-
Literal (
String s = "Hello";
)-
Stored in String Pool.
-
Reuses objects if the same literal already exists.
-
-
new keyword (
String s = new String("Hello");
)-
Always creates a new object in heap (outside pool).
-
Even if
"Hello"
already exists in the pool.
-
🔹 5. intern() Method
-
Moves a string to the pool (or returns reference if already exists).
🔹 6. Memory Layout of String
-
When you create a string, it can exist in two places:
-
Heap (non-pool area) → objects created with
new
. -
Heap (String Pool) → only one instance of each literal.
-
🔹 7. Why Strings are Immutable?
-
Security – Strings are used in class loading, file paths, database connections. If mutable, hackers could change values.
-
Thread Safety – Multiple threads can share strings safely.
-
Caching & Hashing – String is heavily used in HashMap keys. Immutability guarantees consistent
hashCode()
.
🔹 8. String Comparison
-
==
→ compares reference (memory location). -
.equals()
→ compares content.
🔹 9. String Operations
-
Concatenation
-
Methods:
substring()
,toUpperCase()
,toLowerCase()
,replace()
,trim()
, etc. -
Interfaces: String implements
Comparable
andCharSequence
.
🔹 10. Performance Considerations
-
Since String is immutable:
-
Repeated concatenation (+) creates multiple objects → costly.
-
-
Solution:
-
Use
StringBuilder
(faster, non-thread-safe). -
Or
StringBuffer
(slower, thread-safe).
-
🔹 11. Key Points to Remember
-
Strings are immutable.
-
String literals go into the String Pool.
-
new String("abc")
creates object in heap, not in pool. -
Use
intern()
to move string to pool. -
Use
.equals()
for content comparison. -
Use
StringBuilder
/StringBuffer
for heavy modifications.
🔹 12. When to Use (Use Cases)
-
When the value does not change often.
-
For constants, configuration values, keys in maps, messages, tokens, identifiers.
-
When security, caching, or immutability is important.
Examples
-
Example 1: Database URL (Security)
This should never change after creation → String is best.
-
Example 2: HashMap Keys (Immutability)
If keys were mutable, changing them would break the hashcode contract.
🔹 1. What is StringBuilder?
-
StringBuilder is a mutable class introduced in Java 5.
-
Provides an efficient way to manipulate strings (append, insert, delete).
-
Not synchronized → faster, but not thread-safe.
🔹 2. Why StringBuilder?
-
Solves performance issue of immutable Strings.
-
Designed for single-threaded string modifications.
🔹 3. Creating StringBuilder
🔹 4. Mutability Example
🔹 5. Important Methods
-
append(String str)
→ add at end -
insert(int index, String str)
→ insert at position -
replace(int start, int end, String str)
→ replace substring -
delete(int start, int end)
→ delete substring -
reverse()
→ reverse characters -
capacity()
→ total buffer capacity -
length()
→ current number of characters
🔹 6. Capacity Management
-
Default capacity = 16.
-
Expansion formula:
(oldCapacity * 2) + 2
.
🔹 7. Performance Considerations
-
Faster than String and StringBuffer.
-
Ideal for loops and frequent string modifications.
🔹 8. Thread Safety
-
Not thread-safe.
-
If used across threads, must be externally synchronized.
🔹 9. Conversion
🔹 10. Key Points to Remember
-
StringBuilder is mutable.
-
Faster than StringBuffer.
-
Not safe in multithreaded environments.
-
Expands automatically when capacity is exceeded.
🔹 11. When to Use (Use Cases)
-
When working in a single-threaded environment.
-
For heavy concatenation/modification in loops.
-
When performance is critical and thread-safety is not required.
Examples
-
Example 1: Building JSON Response
Efficient, since many modifications happen.
-
Example 2: Generating Report in Loop
Much faster than using String in loops.
🔹 1. What is StringBuffer?
-
StringBuffer is a mutable class introduced in Java 1.0.
-
Similar to StringBuilder but thread-safe (all methods synchronized).
🔹 2. Why StringBuffer?
-
Designed for multi-threaded string modifications.
-
Ensures thread safety with synchronization.
🔹 3. Creating StringBuffer
🔹 4. Mutability Example
🔹 5. Important Methods
(Same as StringBuilder) → append()
, insert()
, replace()
, delete()
, reverse()
, capacity()
, length()
.
🔹 6. Capacity Management
-
Default capacity = 16.
-
Expands as
(oldCapacity * 2) + 2
.
🔹 7. Thread Safety
-
All methods are synchronized.
-
Slower compared to StringBuilder due to synchronization overhead.
🔹 8. Performance Considerations
-
Slower than StringBuilder.
-
Used when multiple threads modify the same string.
🔹 9. Conversion
🔹 10. Key Points to Remember
-
StringBuffer is mutable.
-
Thread-safe due to synchronization.
-
Slower than StringBuilder.
-
Still relevant in legacy applications.
🔹 11. When to Use (Use Cases)
-
When multiple threads might modify the same string.
-
For legacy code (before Java 5).
-
When synchronization is explicitly needed.
Examples
-
Example 1: Multi-threaded Logging System
Thread-safe logging without data corruption.
-
Example 2: Shared Counter (Unsafe with StringBuilder, Safe with StringBuffer)
Synchronization ensures no data inconsistency.
Comparision
Feature | String | StringBuilder | StringBuffer |
Mutability | Immutable | Mutable | Mutable |
Thread Safety | Safe (immutable) | Not safe | Safe (synchronized) |
Performance | Slowest | Fastest | Slower than Builder |
Memory Use | String Pool + Heap | Heap buffer | Heap buffer |
Use Case | Few modifications, constants | Many modifications, single-threaded | Many modifications, multi-threaded |
-
String
Pool: "Hello"
, " World"
Heap: "Hello World"
(new object).
-
StringBuilder
Pool: "Hello"
, " World"
Heap: buffer → modified in-place → "Hello World"
.
-
StringBuffer
Same as StringBuilder but synchronized operations.
Expected Output (approximate, will vary):
-
Time taken by String: 1200 ms
-
Time taken by StringBuilder: 5 ms
-
Time taken by StringBuffer: 10 ms
Explanation
-
String → very slow because each concatenation creates a new object in heap.
-
StringBuilder → fastest because it modifies in-place without synchronization.
-
StringBuffer → slightly slower than StringBuilder because methods are synchronized.
Tip:
“In my test, String concatenation was hundreds of times slower than StringBuilder. That’s why for loops or heavy modifications, we always prefer StringBuilder or StringBuffer.”
Q1. Why are Strings immutable in Java?
Answer:
-
Security → Strings store sensitive data (DB URLs, file paths, class names). If mutable, malicious code could modify them.
-
Caching → String Pool stores unique objects. If mutable, one change would affect all references.
-
Thread Safety → Immutable objects are inherently safe for multi-threading.
-
Hashcode Consistency → Strings are used as keys in
HashMap
. If mutable, hashCode() would change → breaking lookups.
Q2. Difference between ==
and .equals()
for Strings?
-
==
→ compares reference (memory address). -
.equals()
→ compares content.
Q3. How does String Pool work?
-
Special memory area inside Heap.
-
Stores unique string literals.
-
Process:
-
JVM checks pool.
-
If literal exists → reuse reference.
-
If not → create new object in pool.
-
Q4. What does intern()
do?
-
Moves string into the pool (if not already).
-
Returns pooled reference.
Q5. Why is String final in Java?
-
Prevents subclassing → no overriding of
equals()
,hashCode()
. -
Ensures immutability.
-
Provides security + performance consistency.
Q6. Difference: String vs StringBuilder vs StringBuffer
Feature | String | StringBuilder | StringBuffer |
---|---|---|---|
Mutability | Immutable | Mutable | Mutable |
Thread-Safe | Yes | No | Yes |
Performance | Slowest | Fastest | Slower |
Use Case | Few changes | Many changes in single-thread | Many changes in multi-thread |
Q7. Why is String concatenation using +
sometimes efficient?
-
Compiler optimization:
-
But for runtime:
Q8. How many objects will this create?
-
"Hello"
→ String Pool. -
new String("Hello")
→ new Heap object.
Total = 2 objects.
Q9. Can String be used as a key in HashMap? Why?
Yes
-
Because it’s immutable.
-
Hashcode stays consistent → no lookup failures.
Q10. Substring memory leak (before Java 7)?
-
Java ≤ 6 → substring shared same
char[]
as original string. -
Large string → kept in memory even if small substring needed.
-
Java 7+ → substring creates new char[] → no leak.
Q11. Difference: String.valueOf()
vs toString()
-
toString()
→ instance method, can throw NullPointerException. -
String.valueOf()
→ static, returns"null"
if object is null.
Q12. Is String thread-safe?
Yes, because it’s immutable. Multiple threads can safely share the same string.
Q13. Why prefer StringBuilder in loops?
-
String → inefficient, creates multiple objects.
-
StringBuilder → efficient, modifies buffer in-place.
Q14. Difference: new String("abc")
vs "abc"
-
"abc"
→ goes to String Pool. -
new String("abc")
→ always creates new Heap object.
Q15. Can String be made mutable using Reflection?
Yes (hacky). Example → modify internal value[]
via reflection.
But dangerous, breaks immutability guarantees → not recommended.
Q16. Difference: String.join()
vs concat()
-
concat()
→ joins two strings. -
join()
→ joins multiple strings with a delimiter.
Q17. How does String.hashCode()
work?
-
Formula:
-
Uses prime
31
for better distribution & fewer collisions.
Q1. Why prefer StringBuilder over String in loops?
-
String → creates new object on every modification → memory heavy.
-
StringBuilder → modifies in-place → better performance.
Q2. Difference between capacity()
and length()
?
-
length()
→ number of characters stored. -
capacity()
→ total buffer size before resizing.
Q3. Is StringBuilder thread-safe?
No
-
If multiple threads access same instance → must add synchronization manually.
Q4. How does resizing of StringBuilder work?
-
New capacity =
(oldCapacity * 2) + 2
.
Q5. Can we convert StringBuilder to String?
Yes → by calling toString()
.
Q1. Difference between String, StringBuilder, and StringBuffer?
-
String → Immutable.
-
StringBuilder → Mutable, fast, not thread-safe.
-
StringBuffer → Mutable, thread-safe (synchronized), slower.
Q2. When to use StringBuffer?
-
When multiple threads modify the same string.
Q3. Is StringBuffer still relevant today?
-
Rarely used in modern code.
-
Developers often prefer
StringBuilder
with explicit synchronization. -
Still useful in legacy apps.