Java being one of the most popular object-oriented programming language provides various concepts for crating applications.In Java interviews one of most asked question is that what is mutable and immutable and Why String is Immutable in Java ? This question also asked as Why String class is made final in Java or simply, Why String is final ? In order to understand this we must have solid understanding of what is string and what is the architecture behind string in java.
What is Mutability?
The word mutable means changeable. If an object of a class can be changed this class is called mutable class. In another word you can change/update the values of an a objects without creating a another object then it's mutable.
What is Immutability?
The word immutable means unchangeable. If and object of a class cannot be changed, then this class is called immutable class. In another word you cannot change or update the values of and object without creating another object then it's immutable class.
Here are 6 reasons why string is immutable in Java
1) String Pool
If you read this you most likely have used java before then you knows that String going to be most used data type in all kind of java applications and that's why they wanted to optimize from start. Other than creating new memory space for every string string literals and reusing them saves a lot of heap space because different String variables refer to the same object in the Sting pool. String inter pool serves exactly this purpose.
String pool is a storage are in java heap. To decrease the number of String objects created in the JVM, the class keep a pool of Strings. Each and every time String literal is created, the JVM checks literal pool first. If the string is already exists in the string pool, a reference to the pool instance returns. If the string not exist in the pool, a new string object initializes and is place in the pool. By making String immutable, this sharing of String literal was possible. String pool is not possible without string is immutable.
2) Security
Parameters are typically represented as String in network connections, database connection URLs, usernames/passwords, etc. If it was mutable, these parameters could be changed easily.
3) Multithreading Benefits
Since multi-threading was java's key offering, it makes lot of sense to think about thread safety of string objects. making String immutable automatically makes them thread safe thereby solving the synchronization issues.
4) Performance
Now that make class immutable you know in advance that this class is not going to change once created. Because of string is also immutable it turn it enhances the performance by saving heap memory and faster access of hash implementations when operating with Strings. Since strings are used in everywhere including data structures improving performance of String have a considerably performance improvement in whole application.
5) Hashcode Caching
Since String objects are abundantly used as data structure, they are also widely used in hash implementation such as HashMap, HashTable, HashSet, when operating upon these hash implementations hashCode() method is called quite frequently. So the hashCode() method is overridden in String class to facilitate caching, such that the hash is calculated and cached during the first hashCode() call and the same value is returned ever since.
This, in turn, improves the performance of collections that uses hash implementations when operated with String objects.
6) Class Loading
String is used as arguments for class loading. if mutable, it could result in the wrong class being loaded because mutable objects can change their state. By keeping String final and immutable, we can at least be sure that JVM is loading correct classes.
Conclusion
These 6 reasons definitely gives an hint that Why String class has been made Final and Immutable in Java. Of-course it's decision of Java designers but looks like above points contributes to take them this decision. Due to similar reasons wrapper classes like Integer, Long, Double and Float are also immutable and Final. In case you want mutable Strings in your application you can use StringBuilder and StringBuffer classes. StringBuffer and StringBuilder classes are mutable.
“First, solve the problem. Then, write the code.” – John Johnson
Comments
Post a Comment