5 Replies Latest reply: Feb 17, 2009 4:02 PM by 796440 RSS

    Question about String Class

    oais
      Hello ,

      Since Strings are constant; their values cannot be changed after they are created.
      I dont understand why in this code :
      String newStr = new String("hello world");
      newStr = "goodbye world";
      newStr = null;
      
      System.out.println(newStr);
      the value that was printed is "null" not the value that was given first to the newStr which is "hello world"?

      Thanks...
        • 1. Re: Question about String Class
          807588
          constants can be changed, they are not final. The compiler goes down the code, starting from the top then down. So, the last initialization is what the string will be set as. Hope this helps!
          • 2. Re: Question about String Class
            699554
            Strings are immutable, meaning they can not be changed without creating a new string. This is exactly what you are doing when you give a string a new value, you are creating a new string and referencing that new string.

            Mel
            • 3. Re: Question about String Class
              800282
              HeavenBoy wrote:
              Hello ,

              Since Strings are constant; their values cannot be changed after they are created.
              I dont understand why in this code :
              String newStr = new String("hello world");
              newStr = "goodbye world";
              newStr = null;
              
              System.out.println(newStr);
              the value that was printed is "null" not the value that was given first to the newStr which is "hello world"?

              Thanks...
              You first let 'newStr' point to the String "hello world"
              Then you let 'newStr' point to "goodbye world"
              And lastly, you let 'newStr' point to null

              So, it's not surprising that in the end, 'null' is printed. No?
              • 4. Re: Question about String Class
                807588
                You are confusing String objects (which are immutable) and String reference variables (which are mutable unless declared final).
                String newStr = new String("hello world");
                You have a String literal "hello world".
                You create another String object that uses the same backing char array. The original "hello world" String literal still exists and is unchanged.
                You assign it to the String reference variable 'newStr'.
                newStr = "goodbye world";
                You have another String literal "goodbye world".
                You assign it to the String reference variable 'newStr'. Both your "hello world" Strings still exist and are unchanged. One of them may now be eligible for garbage collection.
                newStr = null;
                You assign null to the String reference variable 'newStr'. Both your "hello world" Strings and your "goodbye world" String still exist and are unchanged.

                Edited by: tschodt on Feb 17, 2009 9:57 PM
                • 5. Re: Question about String Class
                  796440
                  tschodt wrote:
                  You are confusing String objects (which are immutable) and String reference variables (which are mutable unless declared final).
                  String newStr = new String("hello world");
                  You have a String literal "hello world".
                  You create another String object that uses the same backing char array. The original "hello world" String literal still exists and is unchanged.
                  You assign it to the String reference variable 'newStr'.
                  In particular, the "it" that gets assigned here and elsewhere is the reference, not the String object. Variables in Java never hold objects, only references that point to objects, or that contain the value null. All other variables are primitives.