6 Replies Latest reply on Dec 18, 2008 5:46 PM by 3004

    String objects with the same string pointing to the same place but?

    843785
      Hi,
      Here is a simple sample
      String str1 = "a";
      String str2 = "a";
      I understand that in Java str1 and str2 pointing to the same place in memory and Java compiler wouldn't save the string "a" in two different places in memory. Am I right?

      If so, if the value of str1 changes to something else as following:
      String str1 = "a";
      String str2 = "a";
      str1 = "b";
      why str2 which has been pointing to the same place as str1 before is still containing ?a? and not ?b????

      Thanks!
        • 1. Re: String objects with the same string pointing to the same place but?
          800282
          Maria1990 wrote:
          Hi,
          Here is a simple sample
          String str1 = "a";
          String str2 = "a";
          I understand that in Java str1 and str2 pointing to the same place in memory and Java compiler wouldn't save the string "a" in two different places in memory. Am I right?
          Correct.
          Maria1990 wrote:
          If so, if the value of str1 changes to something else as following:
          String str1 = "a";
          String str2 = "a";
          str1 = "b";
          why str2 which has been pointing to the same place as str1 before is still containing ?a? and not ?b????
          Because str2 is pointing to the same "a", but it is not pointing to str1.
          This is what happens:
          String str1 = "a";
          /*                 +---+
                   str1 ---->| a |
                             +---+
          */
          
          String str2 = "a";
          /*                 +---+
                   str1 --+->| a |
                          |  +---+
                          |
                   str2 --+
          
          NOT:
                             +---+
               +-> str1 ---->| a |
               |             +---+
               |
               +- str2
          */
          
          str1 = "b";
          /*
                             +---+
                       +---->| b | 
                       |     +---+
                       |
                       |     +---+
                  str1-+  +->| a |
                          |  +---+
                          |
                   str2 --+
          */
          • 2. Re: String objects with the same string pointing to the same place but?
            843785
            Thank you prometheuzz for the well explained answer and even with illustration which made it very easy to understand.
            I have one more question which is bugging me. What would be the reason of using
            String str = new String("a")
            instead of
            String str = "a"
            I understand that new String("a") would make a new object and I have been reading about that it would make a difference when it comes to using the
            operator str1 == str2  OR  str1.equals(str2)
            But when should I use new String("a"). What is it good for? When a programmer see it necessary to use new String("a")?

            Thanks!!!
            • 3. Re: String objects with the same string pointing to the same place but?
              791266
              But when should I use new String("a").
              Almost never. I don't think I've written new String("...") during the last 10 years.
              What is it good for?
              Not much
              When a programmer see it necessary to use new String("a")?
              Probably never
              • 4. Re: String objects with the same string pointing to the same place but?
                800308
                But when should I use new String("a").
                Only ever seen it in generated code, which doesn't take advantage of Strings "special powers".
                • 5. Re: String objects with the same string pointing to the same place but?
                  3004
                  Maria1990 wrote:
                  Thank you prometheuzz for the well explained answer and even with illustration which made it very easy to understand.
                  I have one more question which is bugging me. What would be the reason of using
                  String str = new String("a")
                  instead of
                  String str = "a"
                  There's no reason to ever pass a string literal to new String(String). The only use for the new String(String) constructor that I'm aware of is when you're getting a small substring of a very large string and you don't need the larger string any more.
                  String small = new String(veryLargeString.substring(a small range));
                  If you don't use the c'tor--that is, you just do ... = veryLargeString.substring...--then the both String objects will share the same character array. If you only need the smaller string, you're forcing a large char array to be kept around when you only need a small piece of it, wasting memory. By using the c'tor, the smaller string gets its own array.

                  However, this assumes that particular implementation detail of String, which could change in a future version or a different vendor's VM, and it's only a very unusual corner case where it would even matter.
                  • 6. Re: String objects with the same string pointing to the same place but?
                    3004
                    corlettk wrote:
                    But when should I use new String("a").
                    Only ever seen it in generated code, which doesn't take advantage of Strings "special powers".
                    That would be a fairly stupid code generator, IMHO.