Category:
JAVA STRING INTERVIEW QUESTIONS AND ANSWERS
String is immutable means that once a String object is created , its value cannot be changed.
There are many advantages of Strings being immutable:
- Security: Immutabity of String prevents sensitive data such as passwords , file paths and URLs from being modified after creation.
- Thread Safety: As string is immutable, Multiple Threads can access the same string object without synchronization issuues.
- String Pool Optimization : As String is immutable, it allows multiple references to safely share the same string object , which save memory.
- HashCode Caching: Due to immutable property of String , the hashcode remains constant , which improves the performance of collections like HashMap.
- Better Memory Management : As String is immutable , strings can be reused , which reduce the unnecessary object creation.
