Why String Class is Immutable

Price: INR 1,299.00
Was: INR 1,799.00

The immutable class is a class which created only once and its contents can not be changed. Immutable objects are the objects whose state can not be changed once constructed. e.g. String class

 

How to Create Immutable Class

  1. Create a final class.

  2. Make the class properties final and private.

  3. Set the class properties values using constructor only.

  4. Don’t declare any setter method for the class properties.

Now your class is Immutable.

 

Why String Class is Immutable

In Java, String is immutable class. Immutable means a class that can’t be modified. There are multiple reasons that String is designed to be immutable in Java. There are many reasons why String is Immutable. We will discuss all these in details.

 

Automation Testing Video Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

 

String Pool – String pool is a special storage area in Java heap. Java designer knows that String is going to be most used data type in all kind of Java applications and that’s why they wanted to optimize from the start. When a string is created and if the string already exists in the pool, the reference of the existing string will be returned, instead of creating a new object and returning its reference.

If the string is not immutable, changing the string with one reference will lead to the wrong value for the other references.

   String msg = “Hello, Friend” ;

   String msg1 = “Hello, Friend” ;

String Class

Allow String to use its Hashcode – The hashcode of the string is frequently used in Java. Being immutable guarantees that hash code will always the same so that it can be cashed without worrying the changes.That means, there is no need to calculate hash code every time it is used. This is more efficient.

 

Security –  Java has the clear goal in terms of providing a secure environment at every level of service and String is critical in that whole security stuff. The string has been widely used as the parameter for many Java classes, e.g. for opening network connection, you can pass host and port as String, for reading files in Java you can pass the path of files and directory as String and for opening database connection, you can pass database URL as String.

If String was not immutable, a user might have granted to access a particular file in the system, but after authentication, he can change the PATH to something else, this could cause serious security issues. Similarly, while connecting to a database or any other machine in the network, mutating String value can pose security threats. Mutable strings could also cause the security problem in Reflection as well, as the parameters are strings.

 

A string is Thread-Safe – String is immutable, it is safe for multithreading and a single String instance can be shared across different threads. This avoids the use of synchronization for thread safety, Strings are implicitly threaded safe.

 

String class Loading Mechanism – Another reason for making String final or Immutable was driven by the fact that it was heavily used in class loading mechanism. For example, think of an instance where you are trying to load java.sql.Connection class but the referenced value is changed to my hacked.Connection class that can do unwanted things to your database. By keeping String final and immutable, we can at least be sure that JVM is loading correct classes.

0 Comment

Leave a Reply