Immutability of Strings in Java

后端 未结 26 2333
不思量自难忘°
不思量自难忘° 2020-11-21 06:33

Consider the following example.

String str = new String();

str  = \"Hello\";
System.out.println(str);  //Prints Hello

str = \"Help!\";
System.out.println(s         


        
26条回答
  •  余生分开走
    2020-11-21 07:34

    Lets break it into some parts

    String s1 = "hello";
    

    This Statement creates string containing hello and occupy space in memory i.e. in Constant String Pool and and assigned it to reference object s1

    String s2 = s1;
    

    This statement assigns the same string hello to new reference s2

             __________
            |          |
    s1 ---->|  hello   |<----- s2
            |__________| 
    

    Both references are pointing to the same string so output the same value as follows.

    out.println(s1);    // o/p: hello
    out.println(s2);    // o/p: hello
    

    Though String is immutable, assignment can be possible so the s1 will now refer to new value stack.

    s1 = "stack";    
             __________
            |          |
    s1 ---->|  stack   |
            |__________|
    

    But what about s2 object which is pointing to hello it will be as it is.

             __________
            |          |
    s2 ---->|  hello   |
            |__________|
    
    out.println(s1);    // o/p: stack
    out.println(s2);    // o/p: hello
    

    Since String is immutable Java Virtual Machine won't allow us to modify string s1 by its method. It will create all new String object in pool as follows.

    s1.concat(" overflow");
    
                     ___________________
                    |                   |
    s1.concat ----> |  stack overflow   |
                    |___________________|
    
    out.println(s1);    // o/p: stack
    out.println(s2);    // o/p: hello
    out.println(s1.concat); // o/p: stack overflow
    

    Note if String would be mutable then the output would have been

    out.println(s1);    // o/p: stack overflow
    

    Now you might be surprised why String has such methods like concat() to modify. Following snippet will clear your confusion.

    s1 = s1.concat(" overflow");
    

    Here we are assigning modified value of string back to s1 reference.

             ___________________
            |                   |
    s1 ---->|  stack overflow   |
            |___________________|
    
    
    out.println(s1);    // o/p: stack overflow
    out.println(s2);    // o/p: hello
    

    That's why Java decided String to be a final class Otherwise anyone can modify and change the value of string. Hope this will help little bit.

提交回复
热议问题