Why String is immutable or final in Java? Immutable String in java.

If you are preparing for a java interview then this is one of the most interesting question you will encounter. Immutability means once created can not be changed. If someone changes the string object then new object will be created. For Java world, String Class is heart of Java language. If we talk about another programming languages then we can see that String is most widely used.  The string is immutable and final in Java and java runtime maintains a String constant pool that makes it a very very special class. There are multiple reasons that String is designed to be immutable and final in Java. Here I am going to explain you some of the reasons behind immutable string in Java.

1. String pool(String intern pool) is a special area in heap memory. When a string is created and if the string already exists in the pool then the reference of the already existing string will be returned instead of creating new string object. The following code will create only one string object in the string pool.

String s1 = "topcode";
String s2 = "topcode";

In above example, both of the string reference variable s1 and s2 refer to string “topcode”. If a string is not immutable then changing the string with one reference will lead to the wrong value to other references. In that case, string interning would not be possible because if someone changed the value of one variable then it will be reflected in other as well.

2. Security: If we talk about security then String class is a blessing for java. If String is not immutable then it would cause a drastic security threat to the application. A string is widely used as a parameter for many java classes, e.g. database connection, opening files, socket programming etc. If String is not immutable, a connection or file would be changed and lead to a serious security threat.  Since String is immutable it’s value can’t be changed otherwise an attacker could change the referenced value to cause security issues in the application. A mutable string can cause security issue in your application by use of Java Reflection API. You can create a Mutable string with the help of StringBuffer and StringBuilder class.

3. Since String is immutable it can safely share between multiple threads in the multithreaded environment and also avoid synchronization issues. Immutability also makes String instance thread-safe in Java, means you don’t need to synchronize String operation externally.

4. Strings are also used in java Classloader and immutability gives security that correct class is getting loaded by Classloader. For example, you want to load a class “com.mysql.jdbc.Driver” but the reference value is changed to “com.attacksql.jdbc.Driver” class that can do unwanted things with your database and application.

Connection conn = DriverManager.getConnection("jdbc:mysql://hostname:port/dbname","username", "password");

5. As We know String class overrides equals and hashcode method of object class. Since string is immutable allow string to cache it’s hashcode, and do not calculate every time we call hashcode method of String. It means the hashcode value is calculate and cached at the time of creation and doesn’t need to calculate again. This is why String is mostly used Object as HashMap keys. In short, because String is immutable, no one can change its contents once created which guarantees hashCode of String to be same on multiple invocations.

Security, multithreading and String pool being primary reason of making String immutable. There could be more reason why String is immutable and final. If you want to share your thoughts then please comment your answer. Thanks for reading and stay tuned.👍

, , ,

Related posts

Advantages of Java:


Java is a popular and versatile programming language that has been around since 1995....

What is WebSocket?


WebSockets is a communication protocol that provides a persistent, bidirectional, full-duplex connection between a...

Latest posts

1 comment

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Please disable your adblocker or whitelist this site!

How to whitelist website on AdBlocker?

How to whitelist website on AdBlocker?

  1. 1 Click on the AdBlock Plus icon on the top right corner of your browser
  2. 2 Click on "Enabled on this site" from the AdBlock Plus option
  3. 3 Refresh the page and start browsing the site