There are many reasons due to the string class has been made immutable in Java. These reasons in view, concurrency issues, security issues and performance issues. Here is a list of various valid reasons to go for immutable string class:

Concurrency

1. The use of immutability is a best practice and has been recommended by many sources including effective Java and official Oracle Java tutorials. The property of the object is that none of the member variables that object can be changed as they are marked as private and final. This property becomes an advantage in case of multi-threaded applications. Since the updating of the String object is not allowed, multiple threads cannot run in to synchronization issues which occur when one object updates the state of object while the other was reading the object.

The official Oracle Java tutorial says that developers should not have any doubt by using immutability. Usually developers think that by having immutability the number of objects in the memory will increase because instead of updating an object, a new object has to be created. But in reality this is balanced off by the reduction in garbage collector execution. If properly used immutability can make an application more stable.

Performance

2. The second reason why string class is immutable in Java is both a cause as well as effect of string being immutable. Strings in Java in implement the fly weight design pattern and the result is string literal pool. This literal pool has the property that if string literal is already present in the literal pool then it will be reused every-time a string reference variable is initialized to a string literal with the same characters.

The requirement for having a string literal pool arises from the fact that string class is immutable in Java. Imagine a scenario where millions of string objects have been created with same characters just because the new operator always creates and returns a new instance of the class and we cannot modify a string object once it has been created. The above scenario will result in performance issues. To avoid these performance issue, string literal pool has been introduced in Java.

Now let us see why string literal pool is a cause for making string class immutable in Java. Since the string literals have to be reused, updating the contents of these objects should not be allowed. Had updating of string literals been allowed, a string literal may not be reused because its contents have been changed by another reference variable.

Performance

3. The most used key object for hash map is the string object. Every time a string is referenced in hash based collections classes, it’s hash code is calculated. The hash code of string objects depends upon the characters contained in that string. If the characters of a string object were allowed to be changed, the hash code of that string object will change when the characters of the string change. By making string class immutable in Java, it has been insured that the hash code of string object will not change after the string object has been created in the memory. More over, this allows the hash code to be cached as a member variable. Once the hash code has been calculated for a string object and is told as the value of this internal member variable, the same can be returned next time without the need for performing any calculation.

Security

4. The security aspect of having the string class immutable in Java is that strings are used for file operations, memory management and network operations. If strings are allowed to be mutable, various properties could be changed in malicious ways.

Simplicity

5. Another reason to make the string class mutable is the simplicity aspect. Though for beginners it is a learning curve to understand the behavior of string objects but once they understand, it is very easy to visualize the behavior of string objects in any particular scenario. Allowing the updating of string objects would have made it more complex to understand the behavior of strings.

The point to note about the design decision is that the advantages of making string class immutable are more than not making it immutable.

Related Posts

Why the String class is Immutable admin Core Java
There are many reasons due to the string class has been made immutable in Java. These reasons in view, concurrency issues, security issues and performance issues. Here is a list of various valid reasons to go for immutable string class: Concurrency 1. The use of immutability is a best practice and...
There are many reasons due to the string class has been made immutable in Java. These reasons in view, concurrency issues, security issues and performance issues. Here is a list of various valid reasons to go for immutable string class: <h3>Concurrency</h3> 1. The use of immutability is a <a href="http://www.javaexperience.com/java-best-practice-avoid-final-for-tdd/" title="Best Practice – Avoid Final for TDD">best practice</a> and has been recommended by many sources including effective Java and official Oracle Java tutorials. The property of the object is that none of the member variables that object can be changed as they are marked as private and <a href="http://www.javaexperience.com/initializing-final-variables-in-java/" title="Initializing final variables in Java">final</a>. This property becomes an advantage in case of multi-threaded applications. Since the <strong>updating of the String object is not allowed</strong>, multiple threads cannot run in to synchronization issues which occur when one object updates the state of object while the other was reading the object. The official Oracle Java tutorial says that developers should not have any doubt by using immutability. Usually developers think that by having immutability the number of objects in the memory will increase because instead of updating an object, a new object has to be created. But in reality this is balanced off by the <a href="http://www.javaexperience.com/java-garbage-collection-tutorial/" title="Java Garbage Collection Tutorial">reduction in garbage collector execution</a>. If properly used immutability can make an application more stable. <h3>Performance</h3> 2. The second reason why string class is immutable in Java is both a cause as well as effect of string being immutable. Strings in Java in <a href="http://www.javaexperience.com/design-patterns-flyweight-design-pattern/" title="Flyweight Design Pattern">implement the fly weight design pattern</a> and the result is string literal pool. This literal pool has the property that if string literal is already present in the literal pool then it will be reused every-time a string reference variable is initialized to a string literal with the same characters. The requirement for having a string literal pool arises from the fact that <strong>string class is immutable in Java</strong>. Imagine a scenario where millions of string objects have been created with same characters just because the new operator always creates and returns a new instance of the class and we cannot modify a string object once it has been created. The above scenario will result in performance issues. To <a href="http://www.javaexperience.com/performance-management-for-web-applications/" title="Performance Management in web applications">avoid these performance issue</a>, string literal pool has been introduced in Java. Now let us see why <strong>string literal pool is a cause</strong> for making string class immutable in Java. Since the string literals have to be reused, updating the contents of these objects should not be allowed. Had updating of string literals been allowed, a string literal may not be reused because its contents have been changed by another reference variable. <h3>Performance</h3> 3. The most used key object for hash map is the string object. Every time a string is referenced in hash based collections classes, it's <a href="http://www.javaexperience.com/how-to-generate-hashcode-method-for-a-class-in-java/" title="How to generate hashcode method for a class in Java">hash code is calculated</a>. The hash code of string objects depends upon the characters contained in that string. If the characters of a string object were allowed to be changed, the hash code of that string object will change when the characters of the string change. By making string class immutable in Java, it has been insured that the <a href="http://www.javaexperience.com/java-dive-deep-into-hashcode-internals/" title="Dive deep into Hashcode Internals">hash code of string object</a> will not change after the string object has been created in the memory. More over, this allows the hash code to be cached as a member variable. Once the hash code has been calculated for a string object and is told as the value of this internal member variable, the same can be returned next time without the need for performing any calculation. <h3>Security</h3> 4. The security aspect of having the string class immutable in Java is that strings are used for file operations, memory management and network operations. If strings are allowed to be mutable, various properties could be changed in malicious ways. <h3>Simplicity</h3> 5. Another reason to make the string class mutable is the simplicity aspect. Though for beginners it is a learning curve to understand the behavior of string objects but once they understand, it is very easy to visualize the behavior of string objects in any particular scenario. Allowing the updating of string objects would have made it more complex to understand the behavior of strings. The point to note about the design decision is that the advantages of making string class immutable are more than not making it immutable.
Differences between ANT and Maven
Setting initialcapacity of Vector, ArrayList in Java
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.

Comments

comments