Monday, November 7, 2016

Why String is Immutable in Java

Java Designer knows String is going to be most used data type in all kind of java applications. Therefore they want optimize from start. Here,mutable objects are those objects whose contents can be modified whereas immutable objects can not be modified. In java String class objects are immutable that means once we create String objects we are not allowed to perform any changes in the existing object. If you are trying to perform any changes a new String object will be created this behavior is nothing but immutability  of the string object.

The fallowing are the several benefits of String because it's immutable and final:


  1. It increases security because any hacker can not change it's value and it is used for storing sensitive information such as database username and password etc..
  2. Since String is immutable,it is safe to use in multithreading and we do not need any synchronization .
Importance of String Constant Pool(SCP):

In our program if any string object is repeatedly going to use, we can create only one object in the String Constant Pool and shared by several required references. The main purpose of SCP is instead of creating several string objects with the same content creating only one object improves the performance of the system and memory utilization also.

As the several references pointing to the same object in SCP by using one reference if you are allowed to change the content the remaining references may be effected. Hence,once we created a String Object we are not allowed to change it't content. If you are trying to change with those changes a new object will be created and there is no impact on the remaining references. This behavior is nothing but immutability of String object. SCP is only reason why the string objects are immutable.


Let us take a program to understand whether the string objects are immutable or not

class Demo
{
public static void main(String args[])
{
String s1="java";
String s2="program";
s1=s1+s2;
System.out.println(s1);
}
}

Output:




Observe, the output of the program is "javaprogram". It seems that s1 content is modified. Earlier s1 holds "java" and s2 holds "program". after s1+s2 joined and the total string becomes "javaprogram". This total string assigned to s1 again.That means s1 is mutable. But we said that String objects are immutable.

Do not hesitate String objects are definitely immutable. You people thinking about the output of above program. I will give clarity about it now. In that program, JVM create two objects s1 and s2 separately like below program. When s1+s2 done,JVM creates a new object and stores the string "javaprogram" in that object. But it doesn't modify the content of string s1. After creating the new object, the reference s1 is refer to that new object. We should notice one thing here the contents of the string s1 are not modified. Therefore,strings are immutable.





Since String class objects are immutable,their contents can not be modified between various applications. Thus,different applications can share the contents of String class objects.

Why String object is final:

Whenever we try to modify the contents, JVM will create a new object with modified data. It will not modify the same object, also there should not be any scope to override those string class methods. This is the reason String class if 'final' class. The methods of final class can not be overridden.

For More details about Final class: Final keyword in java

2 comments:

  1. I want the simpler and effective definition for a Class and an Interface?

    ReplyDelete
    Replies
    1. The interface is the pure form of abstraction in java because interface implements 100% abstraction.The interface provides you a method name but not the behavior of the method how it comes from a class which implements it. In a class there may be concrete and abstract methods that is can be both defined and declared methods.

      Delete

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...