Why String is immutable in Java?

String is immutable in Java. An immutable class is simply a class whose instances cannot be modified. All information in an instance is initialized when the instance is created and the information can not be modified. There are many advantages of immutable classes. This article summarizes why String is designed to be immutable. This post illustrate the immutability concept in the perspective of memory, synchronization and data structures.

1. Requirement of String Pool

String pool (String intern pool) is a special storage area in Method Area. 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.

The following code will create only one string object in the heap.

String string1 = "abcd";
String string2 = "abcd";

Here is how it looks:
java-string-pool

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

2. Caching Hashcode

The hashcode of a string is frequently used in Java. For example, in a HashMap or HashSet. Being immutable guarantees that hashcode will always be the same so that it can be cashed without worrying about the changes.That means, there is no need to calculate hashcode every time it is used. This is more efficient.

In String class, it has the following code:

private int hash;//this is used to cache hash code.

3. Facilitating the Use of Other Objects

To make this concrete, consider the following program:

HashSet<String> set = new HashSet<String>();
set.add(new String("a"));
set.add(new String("b"));
set.add(new String("c"));
 
for(String a: set)
	a.value = "a";

In this example, if String is mutable, its value can be changed which would violate the design of set (set contains unduplicated elements). Of curse, the example above is just for demonstration purpose and there is no value field in a real string class.

4. Security

String is widely used as a parameter for many java classes, e.g. network connection, opening files, etc. Were String not immutable, a connection or file would be changed and this can lead to a serious security threat. The method thought it was connecting to one machine, but was not. Mutable strings could cause a security problem in Reflection too, as the parameters are strings.

Here is a code example:

boolean connect(string s){
    if (!isSecure(s)) { 
throw new SecurityException(); 
}
    //here will cause problem, if s is changed before this by using other references.    
    causeProblem(s);
}

5. Immutable objects are naturally thread-safe

Because immutable objects can not be changed, they can be shared among multiple threads freely. This eliminates the requirements of doing synchronization.

In summary, String is designed to be immutable for efficiency and security reasons. This is also the reason why immutable classes are preferred in many cases in general.

20 thoughts on “Why String is immutable in Java?”

  1. You have to assign s1.concat(“JOHN”) to s1 back again, to update its value.


    s1 = s1.concat("JOHN");

    This way s1 will become “JOHNJOHN”

  2. class Check
    {
    public static void main(String j[])
    {
    String s1=”JOHN”;//line no5
    String s2=”JOHN”;//line no6
    //s1.concat(“JOHN”);// line no 7
    //s1=s1+s2; /line no 8
    System.out.println(“Value of S1=”+s1);
    }
    }

    in line no 7 if i compile it then it will print only JOHN but in line no 8 if i compile then it print JOHNJOHN why ?
    can anyone explain please

  3. here foo and bar are pointing to the same memory location contains value “foo” when bar is initialised as “bar” separate memory is located to hold value “bar” . so now bar variable points to the new object contains “bar” value. next print statement foo prints “foo” because foo var points to “foo” object. bar prints “bar” since it points to the bar object. If assuming string is immutable than foo will print “bar” not “foo” because bar and foo variable points to same object hence by using bar variable you changed object value. i hope this helps

  4. In your example, “foo” and “bar” are the immutable string objects, whereas foo and bar are the references to these string object.
    Fortunately, we can swap a value with another one, for a given reference ! That is the reason why bar variable has been assigned the “bar” value after having been assigned the “foo” value first.

    Eventually, immutable means that you are not able to change “bar” value (for example, if you want to add a letter to “bar”, you have no other choice than creating a new instance of a string : you can t reuse the “bar” instance. It causes a more important memory print, but in thé other hand you benefit from the advantages written here (i do not totally agree with security remarks, though).

  5. Please tell me why does this happen and why is it allowed if String is immutable.
    http://ideone.com/6Na6Dq

    String foo = "foo";
    String bar = "foo";
    bar = "bar";
    System.out.println(foo); //prints foo
    System.out.println(bar); // prints bar

  6. Well the string object is thread safe. The reference value is obviously not, since it is mutable. These are two different things in memory.

  7. How can immutable object be a thread safe!? coz if one object makes any changes in reference value, its going to create a new reference only then other thread who is race condition will be referring to the previous reference address and previous reference value.

  8. I don’t agree with the following statement “This is also the reason why immutable classes are preferred in general”

    I do agree immutable has its advantages and makes it easier to develop but most of the developers forget the memory overhead immutable objects bring in to the GC, when we have a immutable hashmap and when its changed, all the references has to recreated and redone for the new variable when we do this multiple times it puts an overhead on the GC and leads to stop the world GC which slows down the programme significantly.

    Where immutable objects makes things easier and is really useful for simple objects like String when it comes to more and more complex objects we need to check simplicity vs performance

  9. a.value = “a”;

    The above will typically work but because strings are objects it performs an object equality. The immutable nature of strings, and the fact that they are cached, makes this incorrect usage of object equality normally work and extremely difficult to figure out when it doesn’t. “a”.equals(a.value) is the error-proof way to compare two strings.

Leave a Comment