• Parth

Why are strings immutable in Java?

In our program if any String object is required repeatedly then it is not recommended to create a separate object for every requirement it affects performance and creates a memory problem.

Instead of creating a separate object for every requirement we can create only one object and we can reuse the same object for every requirement we can achieve this by using SCP(String constant pool) hence the main advantage of SCP is memory utilization and the performance will be improved but in SCP as several references pointing to the same object by using 1 reference if we are allow to change the content then the remaining references will be affected to overcome this problem SUN people implemented String objects as immutable according to this once we create a String object we are not allow to perform any changes in that object if we are trying to perform any changes with those changes a new object will be created hence SCP is the only reason for immutability of String objects.

for getting expertise in JAVA visit Code Planet Technologies Jaipur.