Why String is an immuatable?

String pool (String intern pool) is a special storage area in Java heap. When a string is created and if the string already exists in the pool, the reference of the existing string will be returned, instead of creating a new object and returning its reference.


For example:-


We have an employee named as Pawan, company HR department decided to give the bonus for incredible performance in the project development while firm team in crucial period.


So, for that I need to add bonus to his salary for encouraging him, now if String is not immutable then that bonus will be added to the so many employees of whose named as Pawan, because lot of people exist in a firm/company/organization with same name.



In order to avoid the above problem String is set was an immutable, if it is not immutable the company should lose huge money in term of crores.

No comments:

Post a Comment