Why String is immutable in Java
Java String immutable- String values are stored in String constant pool. It can be accessed by multiple client.
- It is used by the class loading mechanism. For security reason, it should not be changed to other value.
- Immutable string allow to cache its hashcode. It makes hashmap/hashtable very fast since they don’t calculate hashcode everytime.
- String instance is thread-safe in Java.
Reference:
http://javarevisited.blogspot.sg/2010/10/why-string-is-immutable-or-final-in-java.html
Written on October 24, 2017