1 2 3 Previous Next 37 Replies Latest reply: Aug 14, 2007 7:19 AM by 807600 RSS

    why string objects are immutable

    807600
      hi friends
      Can any one explain why strings objects are immutable
      thanks
        • 1. Re: why string objects are immutable
          807600
          Because to save memory, java can refer two string references to the same underlying string object on the heap. therefore if you try to change the string object that more than one reference refers to, it can mess up your program:
          String s1 = "hello";
          String s2 = "hello";
          
          s1.changeValueTo("hi"); //pretend it is a real method
          System.out.println(s2);
          If you could change the string object as above given the way strings are stored, the output might turn out as "hi" since both references could point to the same object.
          • 2. Re: why string objects are immutable
            807600
            Many reasons, here's a good reference:
            http://www.javapractices.com/Topic29.cjp

            If you want a mutable string use StringBuilder or StringBuffer.
            • 3. Re: why string objects are immutable
              807600
              Check out this
              class MuteMyString {
              public static void main(String[] args) throws Exception {
                        String a = "string";
                        Field field = String.class.getDeclaredField("value");
                        field.setAccessible(true);
                        char[] value = (char[]) field.get(a);
                        value[3] = 'u';
                        System.err.println(a);
                        System.err.println(a.equals("string"));
                        
                   }
              }
              I've used a bit of reflection trickery to force the String to mutate (not recommended!) and as a result, the string pool cannot work properly: the runtime believes that the string literals "string" and "strung" are the same thing. Which of course they aren't. If Strings weren't immutable, you couldn't rely on them to be what you think they are

              Immutable objects are good, they're thread-safe and more reliable. Read Josh Bloch's 'Effective Java' for more on this
              • 4. Re: why string objects are immutable
                807600
                Furthermore, that reflection trick shown by georgemc won't work if a proper SecurityManager is in place.

                Message was edited by:
                dwg
                • 5. Re: why string objects are immutable
                  807600
                  Furthermore, that reflection trick shown by georgemc
                  won't work it a proper SecurityManager is in place.
                  Nor on certain JVMs. It takes advantage of the fact that current implementations of java.lang.String use a char[] internally to represent the data, but the JLS doesn't mandate this, and vendors are free to do it however they wish. I just used it to show a danger of having mutable strings. The runtime can be tricked into thinking 2 different strings are in fact identical
                  • 6. Re: why string objects are immutable
                    807600
                    please give me a simple example i am a begineer new to java
                    • 7. Re: why string objects are immutable
                      807600
                      please give me a simple example i am a begineer new
                      to java
                      How much more simple do you want? You've been given a number of reasons, and also shown that if strings were mutable, the runtime could end up thinking that 2 different string literals are the same
                      • 8. Re: why string objects are immutable
                        807600
                        please give me a simple example i am a begineer new to java
                        Reply #1 is the simple example you want.
                        • 9. Re: why string objects are immutable
                          807600
                          please give me a simple example i am a begineer new
                          to java

                          Reply #1 is the simple example you want.
                          I'm afraid reply#1 has nothing to do with immutability.
                          • 10. Re: why string objects are immutable
                            807600
                            I'm afraid reply#1 has nothing to do with immutability.
                            I'm afraid you're wrong.
                            • 11. Re: why string objects are immutable
                              807600
                              I'm afraid reply#1 has nothing to do with
                              immutability.

                              I'm afraid you're wrong.
                              Care to explain why?
                              • 12. Re: why string objects are immutable
                                807600
                                I'm afraid reply#1 has nothing to do with
                                immutability.
                                Please explain? I am studying for the SCJP exam and that's the explanation you are given in what I gather is the most popular study book.
                                • 13. Re: why string objects are immutable
                                  807600
                                  I'm afraid reply#1 has nothing to do with
                                  immutability.
                                  Please explain? I am studying for the SCJP exam and
                                  that's the explanation you are given in what I gather
                                  is the most popular study book.
                                  In your snippet, what's changing is a reference to a string, not the string itself
                                  • 14. Re: why string objects are immutable
                                    807600
                                    Care to explain why?
                                    Reply #1 talks about String immutability to me. But if you have other explanations, I'd be happy to learn from people more experienced than me. No sarcasm.
                                    1 2 3 Previous Next