-2
public class HelloWorld{

     public static void main(String []args){
         
        String s="java";
         s="world";
         
        System.out.println(s);
     }
}

Output:

world

jps
  • 20,041
  • 15
  • 75
  • 79
  • 1
    You replace the String your variable points to, you don't change the String itself – Stultuske Jan 30 '20 at 07:48
  • 1
    Does anybody know of a more specific dupe for this question? This "how can I reassign a string variable if string is immutable"-type question seems to come up a lot. – Andy Turner Jan 30 '20 at 07:55
  • I would liken this to having friends' numbers stored in your phone: if you change the number you have stored, it doesn't change your friend's phone number (which is immutable), it just calls a different phone. – Andy Turner Jan 30 '20 at 07:57

1 Answers1

2

You have two String objects in that code: "java" and "world". Each of them is immutable (unless you use reflection and rely on JDK internals). The s variable first points to the first one, then to the second one, but they're separate objects.

After this:

String s="java";

you have something like this in memory:

               +−−−−−−−−−−+
s:Ref3243−−−−−>| (string) |
               +−−−−−−−−−−+      +−−−−−−−−−+
               | value:   |−−−−−>| (array) |
               | ...      |      +−−−−−−−−−+
               +−−−−−−−−−−+      | 0: 'j'  |
                                 | 1: 'a'  |
                                 | 2: 'v'  |
                                 | 3: 'a'  |
                                 +−−−−−−−−−+

Then after

s="world";

you have:

               +−−−−−−−−−−+
               | (string) |
               +−−−−−−−−−−+      +−−−−−−−−−+
               | value:   |−−−−−>| (array) |
               | ...      |      +−−−−−−−−−+
               +−−−−−−−−−−+      | 0: 'j'  |
                                 | 1: 'a'  |
                                 | 2: 'v'  |
                                 | 3: 'a'  |
                                 +−−−−−−−−−+
               +−−−−−−−−−−+   
s:Ref6449−−−−−>| (string) |   
               +−−−−−−−−−−+      +−−−−−−−−−+
               | value:   |−−−−−>| (array) |
               | ...      |      +−−−−−−−−−+
               +−−−−−−−−−−+      | 0: 'w'  |
                                 | 1: 'o'  |
                                 | 2: 'r'  |
                                 | 3: 'l'  |
                                 | 4: 'd'  |
                                 +−−−−−−−−−+

So the println at the end shows the contents of the second string.


The value member shown above may or may not be called value in any given JDK implementation. It's a private data member.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Sir your point :- (unless you use reflection and rely on JDK internals) can you please expain it in details or any refrence. – Firoz Patel Jan 30 '20 at 07:49
  • 2
    @FirozPatel - That's a side point and you should pretend for the most part that you don't know that. But basically, inside the `String` class there's a private data member that contains a `char[]` of the string's contents. With reflection you can access and change that private data member. You shouldn't, but it's technically possible. The name of that member is not necessarily consistent across different JDKs. Strings are officially immutable. – T.J. Crowder Jan 30 '20 at 07:52