String is immutable because several benefits that were designed around the String class depends on the immutability property of string in java. For example,
1) String Pool
In Java String class is implemented along with the concept of String Pool. String Pool contains the references of various String objects which corresponds to the String literals you are using in your code.
The main purpose of String Pool is to provide a way to share the String objects with the same value and thus preventing the creation of new String objects for those String lierals that have same values.
Each String object in string pool is shared at several places in code. If Strings in Java were to be mutable then a change made to a string object in the pool at one place would affect all other places where that string object is being shared.
For example, suppose you have a code:
String inputFile="orders.dat";
String outputFile="orders.dat";
...
sendDataToFile(String outputFile);
...
Now both inputFile and outputFile would be pointing to same String object with value orders.dat and this object would be in pool.
Now if you change inputFile like this:
inputFile.replace("orders","ordersByMark");
and if Strings would be mutable then the above statement would change the contents of original String object lying in pool from orders.dat to ordersByMark.dat. Now since both inputFile and outputFile was pointing to same String object, change to input file would also change the output file to ordersByMark.dat which is undesirable. Due to this sendDataToFile(String outputFile) method would send data to wrong file.
Thus conclusion is that String pool is meant for sharing the String objects.But this could not become possible if Strings would be mutable.
2) Thread-Safety
One of the greatest advantage of immutable objects is that they can be easily used in mutithreaded environments. Since the state of immutable objects cannot be changed they can be safely used when multiple threads are working upon them. This removes the need of synchronization which is a costly operation.
Since Strings are immutable they can also be used safely in multithreaded environments without requiring sychronization and thus causing performance gains as synchronization affects performance.
Thus due to immutability of Strings they are thread-safe.
3) String used as keys in Maps
Strings are often used as keys in Maps. As you may know that maps calculate hashcode of the keys in order to find the bucket to store that key-value pair.
According to Java API docs of String class:
Clearly hashcode of string is based on the contents of string.
Suppose you are using a string as key in your Map. If Strings were to be mutable and later if you change the contents of that string then hashcode of that key would also get changed. Then Map will be unable to find the bucket for that key and your key-value pair will be lost.
String key contents changed------>hashcode change-------> bucket info lost in Maps for that key.
4) Cahcing the hashcode
Since hashcode of strings in Java depends on its contents and contents of string do not change (immutable), String class caches the hashcode of String objects instead of calculating it again and again.
This causes performance gain where hashcode is needed to be calcuated again and again such as keys in Maps.Thus use of string as keys in Maps also cause performance gains.
1) String Pool
In Java String class is implemented along with the concept of String Pool. String Pool contains the references of various String objects which corresponds to the String literals you are using in your code.
The main purpose of String Pool is to provide a way to share the String objects with the same value and thus preventing the creation of new String objects for those String lierals that have same values.
Each String object in string pool is shared at several places in code. If Strings in Java were to be mutable then a change made to a string object in the pool at one place would affect all other places where that string object is being shared.
For example, suppose you have a code:
String inputFile="orders.dat";
String outputFile="orders.dat";
...
sendDataToFile(String outputFile);
...
Now both inputFile and outputFile would be pointing to same String object with value orders.dat and this object would be in pool.
Now if you change inputFile like this:
inputFile.replace("orders","ordersByMark");
and if Strings would be mutable then the above statement would change the contents of original String object lying in pool from orders.dat to ordersByMark.dat. Now since both inputFile and outputFile was pointing to same String object, change to input file would also change the output file to ordersByMark.dat which is undesirable. Due to this sendDataToFile(String outputFile) method would send data to wrong file.
Thus conclusion is that String pool is meant for sharing the String objects.But this could not become possible if Strings would be mutable.
2) Thread-Safety
One of the greatest advantage of immutable objects is that they can be easily used in mutithreaded environments. Since the state of immutable objects cannot be changed they can be safely used when multiple threads are working upon them. This removes the need of synchronization which is a costly operation.
Since Strings are immutable they can also be used safely in multithreaded environments without requiring sychronization and thus causing performance gains as synchronization affects performance.
Thus due to immutability of Strings they are thread-safe.
3) String used as keys in Maps
Strings are often used as keys in Maps. As you may know that maps calculate hashcode of the keys in order to find the bucket to store that key-value pair.
According to Java API docs of String class:
The hash code for a String object is computed as
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation.
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation.
Clearly hashcode of string is based on the contents of string.
Suppose you are using a string as key in your Map. If Strings were to be mutable and later if you change the contents of that string then hashcode of that key would also get changed. Then Map will be unable to find the bucket for that key and your key-value pair will be lost.
String key contents changed------>hashcode change-------> bucket info lost in Maps for that key.
4) Cahcing the hashcode
Since hashcode of strings in Java depends on its contents and contents of string do not change (immutable), String class caches the hashcode of String objects instead of calculating it again and again.
This causes performance gain where hashcode is needed to be calcuated again and again such as keys in Maps.Thus use of string as keys in Maps also cause performance gains.
I would like to know your comments and if you liked the article then please share it on social networking buttons.
Thanks nice answer…if anybody wants to know the interview questions with answers then please go through this blog http://adnjavainterview.blogspot.in/2014/06/why-string-is-immutable-or-final-in-java.html
ReplyDelete