Diagram to show Java String’s Immutability

Here are a set of diagrams to illustrate Java String's immutability.

1. Declare a string

The following code initializes a string s.

String s = "abcd";

The variable s stores the reference of a string object as shown below. The arrow can be interpreted as "store reference of".

String-Immutability-1

2. Assign one string variable to another string variable

The following code assign s to s2.

String s2 = s;

s2 stores the same reference value since it is the same string object.

String-Immutability-2

3. Concat string

When we concatenate a string "ef" to s,

s = s.concat("ef");

s stores the reference of the newly created string object as shown below.

string-immutability

Summary

In summary, once a string is created in memory(heap), it can not be changed. All methods of String do not change the string itself, but rather return a new String.

If we need a string that can be modified, we will need StringBuffer or StringBuilder. Otherwise, there would be a lot of time wasted for Garbage Collection, since each time a new String is created. Here is an example of using StringBuilder.

Category >> Basics >> Diagram  
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. Shan on 2013-7-11

    why did JAVA design String class this way? what is the purpose?

  2. Sergiy Skynin on 2013-8-29

    the diagram is not very well. String is immutability for perfomance operations like as substring. The string have array of char (value), and offset, count.
    s3 = s.substring(0, 1)

    s3.value reference to “abcdef” in heap
    but s3.offset = 0, count = 1

    s3 reference “a”

  3. ryanlr on 2013-9-10

    No, it will creat a new one by using array copy.

  4. ryanlr on 2013-9-10
  5. Sergiy Skynin on 2013-9-11

    source from rt.jar:
    substring(beginIndex, count)
    return new String(offset + beginIndex, endIndex – beginIndex, value);

    // Package private constructor which shares value array for speed.
    String(int offset, int count, char value[]) {
    this.value = value;
    this.offset = offset;
    this.count = count;
    }

  6. ryanlr on 2013-9-22

    Thanks very much for pointing out this problem. In JDK 7, it actually crates a new array by using array copy. http://www.programcreek.com/2013/09/the-substring-method-in-jdk-6-and-jdk-7/

  7. […] 1. 字符串不变性(String Immutability) […]

  8. […] 1.  字符串不变性 […]

  9. […] 1.  字符串不变性 […]

  10. […] 1.  字符串不变性 […]

  11. […] 1.  字符串不变性 […]

  12. […] 1. 字符串不变性(String Immutability) […]

  13. 图说Java « 优大网 on 2013-10-29

    […] 1. String Immutability […]

  14. […] 1. String Immutability […]

  15. […] 1. 字符串不变性(String Immutability) […]

  16. […] 1. 字符串不变性(String Immutability) […]

  17. Guest on 2013-10-30

    This diagram works for .Net as well. The only change would be s=s+”ef”;

  18. Guest on 2013-10-30

    My bad. I thought I was still on DZone.com (which talks about multiple platforms). This is a Java specific site, so I’ll delete this .Net nonsense.

  19. […] 参考网址:http://www.programcreek.com/2009/02/diagram-to-show-java-strings-immutability/ […]

  20. […] problem raised from the first code fragment is nothing related with string immutability. Even if String is replaced with StringBuilder, the result is still the same. The key point is that […]

  21. […] Diagram to show Java String's Immutability […]

  22. […] problem raised from the first code fragment is nothing related with string immutability. Even if String is replaced with StringBuilder, the result is still the same. The key point is that […]

  23. 939347507 on 2013-11-10

    顶一个nice article

  24. kai ran on 2013-11-25

    Why two s in the last diagram? Where is s2?

  25. […] What is string immutability? […]

  26. […] problem raised from the first code fragment is nothing related with string immutability. Even if String is replaced with StringBuilder, the result is still the same. The key point is that […]

  27. […] What is string immutability? […]

  28. […] What is string immutability? […]

  29. […] What is string immutability? […]

  30. […] 1.  字符串不变性  […]

  31. Danny Suen on 2014-7-1

    Why is variable s2 in the third diagram?

  32. juyu on 2014-7-5

    i think that the top “s” in the last diagram should be “s2”.

  33. […] 1、字符串不变性 […]

  34. […] 1、字符串不变性 […]

  35. […] 1、字符串不变性 […]

  36. […] 1、字符串不变性 […]

  37. […] append()方法相反于星巴克例子中的一个步骤,toString()方法是个中的最初一步。从这幅图,我们很随意疏忽理解String为什么是弗成变的。 […]

  38. […] 外文链接:Diagram to show Java String’s Immutability […]

  39. […] 外文链接:Diagram to show Java String’s Immutability […]

  40. […] 1、字符串不变性 […]

  41. […] 1、字符串不变性 […]

  42. […] 1、字符串不变性 […]

  43. […] 字符串是不变的,这也就意味着字符串一旦被创建,它们将一直保持在那直到被垃圾回收器清理为止。而对于一个数组,你可以明确的修改它的元素。这样一来,安全性敏感信息(比如密码)将不会出现在系统的任何其它地方。 […]

  44. […] 1、字符串不变性 […]

  45. […] 1、字符串不变性 […]

  46. pavlozvarych on 2015-11-6

    I learn Java for year, but only now understood what is it “immutable” String. Thnx

  47. […] Strings are immutable, which means once they are created, they will stay unchanged until Garbage Collector kicks in. With an array, you can explicitly change its elements. In this way, security sensitive information(e.g. password) will not be present anywhere in the system. […]

  48. […]   字符串是不变的,这也就意味着字符串一旦被创建,它们将一直保持在那直到被垃圾回收器清理为止。而对于一个数组,你可以明确的修改它的元素。这样一来,安全性敏感信息(比如密码)将不会出现在系统的任何其它地方。 […]

  49. 谭杰伟 on 2016-3-11

    thanks…

  50. Rahul R on 2016-8-16

    s = “abcd”
    s = s.concat(“ef”)
    2 s

Leave a comment

*