49. Why String is immutable?

Why String is immutable?

String is immutable because of somes below reasons:

  • String Pool: String pool is possible if only String is immutable. If String is mutable, changing the String with one reference will lead to change with all references
  • Security: Spring parameter often used in network connection, database URL, username and password. Because String is immutable, this value cannot be changed, otherwise hackers can’t change the important information like username and password, they will take the program permission
  • Muitithread: Because String is immutable, it’s safe for multiple thread.
  • Caching: String is often used for caching purposes and beacause String is immutable, we don’t worry ablout that value can be changed.

Leave a Reply

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