Why String Is Immutable In Java

A String is an essential data type commonly used in programming applications. It is utilized to hold information such as user identifiers, passwords, and more. In Java, String instances are immutable, indicating they cannot be altered or modified once created.

After a String object is created, its data or state cannot be modified, but a new String object is generated instead.

Let's explore the idea of immutability through the following example:

Testimmutablestring.java

Example

class Testimmutablestring{

 public static void main(String args[]){

   String s="Sachin";

   s.concat(" Tendulkar");//concat() method appends the string at the end

   System.out.println(s);//will print Sachin because strings are immutable objects

 }

}

Output:

Output

Sachin

The concept can be clarified with the diagram provided below. In this scenario, Sachin remains unchanged, while a new object is generated with the name Sachin Tendulkar. This is the reason why String is referred to as immutable.

In the illustration above, it is evident that two objects have been instantiated, yet the reference variable continues to point to "Sachin" rather than "Sachin Tendulkar".

If we specifically assign it to the reference variable, it will point to the object representing "Sachin Tendulkar".

For example:

Testimmutablestring1.java

Example

class Testimmutablestring1{

 public static void main(String args[]){

   String s="Sachin";

   s=s.concat(" Tendulkar");

   System.out.println(s);

 }

}

Output:

Output

Sachin Tendulkar

In this scenario, the variable s is pointing to the object representing "Sachin Tendulkar". It's important to note that the Sachin object itself remains unchanged.

Why String objects are immutable in Java?

In Java, the concept of String literal is utilized. For instance, if there are 5 reference variables pointing to the same object "Sachin", modifying the object's value using one reference variable will impact all other reference variables. This behavior underscores the immutability of String objects in Java.

Here are several characteristics of String that contribute to the immutability of String objects:

  • Strings are immutable because once a String object is created, its value cannot be changed.
  • String objects in Java are constant and cannot be modified once they are created.
  • In Java, any operation that appears to modify a String actually creates a new String object.
  • The immutability of String objects in Java helps in creating efficient and secure code.

ClassLoader:

In Java, a ClassLoader accepts a String object as input. It is important to note that if the String object is mutable, its value can be altered, potentially resulting in the loading of a different class than intended.

In order to prevent such misinterpretations, it is important to remember that Strings are immutable.

  1. Ensures Thread Safety:

Because the String object is unchangeable, there is no need to manage synchronization when sharing the object among various threads.

  1. Safety:

In the context of class loading, immutable String objects play a crucial role in preventing errors by ensuring the correct class is loaded, thereby enhancing the security of the application program. To illustrate, in a banking software scenario, the immutability of String objects prevents any unauthorized modifications to the username and password, thus bolstering the security of the application program.

  1. Heap Space:

The unchangeable nature of String contributes to reducing the consumption of heap memory. Every time a new String instance is created, the Java Virtual Machine (JVM) verifies if the value is already present in the String pool. If the value is found, it is simply assigned to the new object. This capability enables Java to optimize the utilization of heap memory.

Why String class is Final in Java?

The decision to make the String class final ensures that its methods cannot be overridden by subclasses. This design choice allows consistent features to be maintained for both existing and future String objects.

Input Required

This code uses input(). Please provide values below: