1 2 Previous Next 28 Replies Latest reply: Mar 6, 2007 11:08 PM by 3004 RSS

    Immutable property of String

    807599
      How is "once initiated, a String object is immutable" expressed in codes?

      I tried the following codes:
      public class App{
        public static void main(String[] args){
            String x = "abc";
            System.out.println(x);
      
            x = "xyz";
            System.out.println(x);
        }
      }
      Output:
      abc
      xyz

      Why is the reference variable x able to be reassigned?

      Thanks.
        • 1. Re: Immutable property of String
          807599
          The String object is immutable. You can change the reference. That's why this does not work:
          String s = "abc";
          System.out.println(s);
          
          s.toUpperCase();
          System.out.println(s);
          • 2. Re: Immutable property of String
            807599
            Thanks. I still do not get the idea of "immutable"

            I tried this:
            String s = "abc";
            System.out.println(s);
             
            System.out.println(s.toUpperCase());
            Output:
            abc
            ABC

            so what does this mean? Why is toUpperCase() provided for the String object if it should not work?

            Thanks.

            Or is there a more obvious example for a beginner?

            Thanks.
            • 3. Re: Immutable property of String
              807599
              String s = "abc";
              System.out.println(s);
              System.out.println(s.toUpperCase());
              System.out.println(s);
              Try this. If String is mutable the output should be:
              abc
              ABC
              ABC

              But the answer is not.
              • 4. Re: Immutable property of String
                807599
                When it says that Strings are immutable it means that a String object once it has been created cannot be changed. I repeat the OBJECT cannot be changed.
                String s = "abc";
                s = "xyz";
                Here, s is a variable not an object. However it references a String object. That object being the letters "abc". On the second line you do not change the "abc" String object, that still exists however the variable s now refers to a second newly created String object being the letters "xyz".
                • 5. Re: Immutable property of String
                  3004
                  The state (contents) of the String object cannot change after creation. You can cause a String reference variable to point to a different String object, and you can call toUppercase, which returns a new String object, but you can't change which characters are held by a String after it's created.
                  • 6. Re: Immutable property of String
                    807599
                    Thanks flounder, got that part.

                    Icycool: I tried this:
                    String str = "abc";
                    System.out.println(str);
                    
                    str = str.toUpperCase();
                    System.out.println(str);
                    Output:
                    abc
                    ABC

                    so it does show that str is "mutable" (I know that String object is immutable :-) ) Any other examples? Thanks.
                    • 7. Re: Immutable property of String
                      3004
                      String s = "abc";
                      System.out.println(s);
                      System.out.println(s.toUpperCase());
                      System.out.println(s);
                      Try this. If String is mutable the output should be:
                      abc
                      ABC
                      ABC
                      If string were mutable AND toUpperCase was written to modify the String object in place. String could be mutable and toUppercase still return a new String.
                      • 8. Re: Immutable property of String
                        3004
                        Thanks flounder, got that part.

                        Icycool: I tried this:
                        String str = "abc";
                        System.out.println(str);
                        
                        str = str.toUpperCase();
                        System.out.println(str);
                        Output:
                        abc
                        ABC

                        so it does show that str is "mutable" (I know that
                        String object is immutable :-) ) Any other
                        examples? Thanks.
                        His point was that if String were mutable and toUppercase was written to modify the string in place, then you'd see str's value change without doing the str= reassignment.
                        • 9. Re: Immutable property of String
                          807599
                          No you haven't got it.Strings are NOT mutable, they CANNOT change. Calling toUppercase or toLowercase returns a new completely different String. So "abc" still exists unchanged and now there is a new "ABC" string.
                          • 10. Re: Immutable property of String
                            3004
                            Nevermind. I misread.

                            Message was edited by:
                            jverd
                            • 11. Re: Immutable property of String
                              3004
                              Thanks flounder, got that part.

                              Icycool: I tried this:
                              String str = "abc";
                              System.out.println(str);
                              
                              str = str.toUpperCase();
                              System.out.println(str);
                              Output:
                              abc
                              ABC

                              so it does show that str is "mutable"
                              The variable str being mutable is not the issue. All variables are "mutable" if they're not declared final. However, "mutable" is a term we use for characterizing objects, not variables. Here, all we did is point the reference variable str to a diferent String object.

                              When we talk about Strings being immutable, we mean the contents of the object cannot change. That's a property of the class, and applies to ALL String objects. If you were to talk about "mutability" of a variable--such as str, above--the only thing that would make sense would be whether it's final, and tha applies to individual variables. It's not a property of any family of variables (except those explicitly declared final).

                              Message was edited by:
                              jverd
                              • 12. Re: Immutable property of String
                                807599
                                Any bets on how long it takes the next person to ask this question?
                                • 13. Re: Immutable property of String
                                  3004
                                  Any bets on how long it takes the next person to ask
                                  this question?
                                  Pointless. My system clock is only accurate to about 10 ms.
                                  • 14. Re: Immutable property of String
                                    807599
                                    Test this program:

                                    public static void main(String args[]) {
                                              String str="1234";
                                              System.out.println("str before is ........"+str);
                                              str.concat("abc");
                                              System.out.println("str after is ........."+str);
                                              
                                              StringBuffer s=new StringBuffer("abcd");
                                              System.out.println("String buffer before is ........."+s);
                                              s.append("123");
                                              System.out.println("String buffer after is........."+s);
                                              

                                              
                                         }

                                    Out Put is:
                                    tr before is ........1234
                                    str after is .........1234
                                    String buffer before is .........abcd
                                    String buffer after is.........abcd123
                                    1 2 Previous Next