What does immutable mean in the context of String in Java?
Immutable means once a String object is created, its value cannot be changed.
Is String immutable in Java?
Yes. String objects cannot be modified after creation.
What happens when you modify a String?
A new String object is created instead of changing the original.
Example of String immutability?
String s = “A”; s = s + “B”; creates a new object.
Why was String made immutable?
For security, performance, and thread safety.
How does immutability improve security?
It prevents modification of sensitive data like file paths or class loaders.
How does immutability improve thread safety?
Immutable objects can be safely shared between threads.
How does immutability help String pool?
It allows reuse of String literals safely.
Does String provide methods like replace() or concat()?
Yes, but they return new String objects.
Can you change characters inside a String?
No. Its internal value cannot be altered.
Which class is mutable alternative to String?
StringBuilder and StringBuffer.
Does immutability reduce memory usage?
It allows String pooling and reuse of identical literals.
What is String pool?
A memory area where identical String literals are reused.
Can immutable objects be subclassed?
No. String is final to preserve immutability.
Key rule to remember?
String is immutable — modification creates a new object.
Why is a String object considered immutable in Java?
Because once a String is created, its internal value cannot be changed.
What happens when you modify a String variable?
A new String object is created instead of modifying the existing one.
Why is immutability important for String literals?
Multiple references can safely share the same literal in the String pool.
What would happen if String were mutable?
Changing one reference would affect all other references pointing to the same object.
How does String pooling relate to immutability?
It allows identical literals to reuse the same memory safely.
Example of shared literal?
String a = “Test”; String b = “Test”; both point to same object.
If a is reassigned, does b change?
No. A new object is created and b still references the original.
Why does immutability improve memory efficiency?
Because shared literals do not require duplicate storage.
How does immutability improve security?
It prevents modification of sensitive data like file paths or credentials.