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.

Category >> Basics >> Diagram >> Java  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>

  1. lordstark on 2013-11-9

    amazingly simple..

  2. Bernie Zhang on 2013-11-21

    Why String pool in Java Heap? Why not Method?

  3. 王正一 on 2013-11-22

    I think String pool in Method Area, not in Heap

  4. iMaplezhou on 2013-11-28

    String Pool is in Method Area, not heap

  5. niu on 2013-12-9

    what a amazing.

  6. Javin Paul on 2014-5-12

    From Java 7 String pool is on heap space rather than permgen space. By the way, I have also shared my thoughts about String class in Java, you may find it useful.

  7. David Johnston on 2014-5-13

    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.

  8. jasondevj on 2014-5-14

    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. Ashish Thakran on 2014-7-14

    Please check out the below link. I think it may be some useful:

    http://newtechnobuzzz.blogspot.in/2014/07/why-string-is-immutable-or-final-in-java.html

  10. Siddhartha Gupta on 2014-9-19

    Excellent article

  11. peacekeeper on 2014-10-29

    Good Article

  12. Aj on 2015-3-31

    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.

  13. Rajinder Singh on 2015-7-25
  14. Roger on 2015-9-3

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

  15. ankur arora on 2016-4-14

    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

  16. Nicolas on 2016-4-27

    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).

  17. Srinidhi Skanda on 2016-7-15

    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

  18. Mohit on 2016-12-27

    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

  19. 여현호 on 2017-3-2

    Great summary about why string is actually immutable type… Thanks

  20. Aydın Mehmet ÖZKAN on 2017-5-21

    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”

Leave a comment

*