5.Q.What are the Advantages of Strings Being Immutable?

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:

  1. Security: Immutabity of String prevents sensitive data such as passwords , file paths and URLs from being modified after creation.
  2. Thread Safety: As string is immutable, Multiple Threads can access the same string object without synchronization issuues.
  3. String Pool Optimization : As String is immutable, it allows multiple references to safely share the same string object , which save memory.
  4. HashCode Caching: Due to immutable property of String , the hashcode remains constant , which improves the performance of collections like HashMap.
  5. Better Memory Management : As String is immutable , strings can be reused , which reduce the unnecessary object creation.

Leave a Reply

Your email address will not be published. Required fields are marked *