Why String is immutable in JavaJava 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.
Written on October 24, 2017