1 2 Previous Next 21 Replies Latest reply: Aug 1, 2007 4:03 PM by 807600 Go to original post RSS
      • 15. Re: strings
        807600
        String str = "rome";
        str = "florence";
        String str2 = "rome";
        str at first was pointing to the object "rome".
        str2 is now pointing to an object "rome"
        is str2 pointing to the exact same object that str
        was aimed at before, or is there a separate object
        with the same value? my guess is the objects are
        different but i'm not sure.
        from the original post, that is how I interpreted it.
        • 16. Re: strings
          807600
          > I made a drawing of it:

          Minor nitpick: The str3 reference won't point to a second instance of "rome" in the string pool, which is what it looks like your diagram portrays. It will point to a new non-pooled string object.

          ~
          • 17. Re: strings
            807600
            At the end:
            str1 == str2 (true)
            str1 == str3 (false)
            str1.equals(str3) (true)
            Prometheuzz, your drawing is great and really helped me understand the internals of what is going on with these string assignments. However, I tested your code and my results were different; I got
            str1 == str2          (false)
            .

            my entire function:
            public static void main(String[] args) {
                    // TODO code application logic here
                    String str = "rome"; 
                    String str1 = str;
                    str = "florence";
                    String str2 = "rome";  // "rome" was already present, so it gets 'recycled'.      
                    String str3 = new String("rome");
                    
                    //str1 == str2        (true)
                    System.out.println("str1 == str2: " + str1 == str2);
                    // my result - FALSE
                    
                    //str1 == str3        (false)
                    System.out.println("str1 == str3: " + str1 == str3);
                    // my result - false
                    
                    //str1.equals(str3)   (true)
                    System.out.println("str1.equals(str3): " + str1.equals(str3));
                    // my result - true
                }
            • 18. Re: strings
              800282
              > I made a drawing of it:

              Minor nitpick: The str3 reference won't point to a
              second instance of "rome" in the string pool, which
              is what it looks like your diagram portrays. It will
              point to a new non-pooled string object.

              ~
              You are right, I had to draw it outside the pool. Thanks for the correction.
              • 19. Re: strings
                807600
                > System.out.println("str1 == str2: " + str1 == str2);

                Due to left-to-right association, the compiler reads that as:
                System.out.println(("str1 == str2: " + str1) == str2);
                Since "str1 == str2: rome" doesn't point to the same object as "rome" (i.e., the results of the expressions on either side of the == operator are different reference values), the expression is false.

                ~
                • 20. Re: strings
                  800282
                  Prometheuzz, your drawing is great and really helped
                  me understand the internals of what is going on with
                  these string assignments.
                  You're welcome.

                  However, I tested your
                  code and my results were different; I got
                  ...
                  Your Strings are probably getting concatenated. Try to place some braces around them:
                  Not:
                  System.out.println("str1 == str2: " + str1 == str2);
                  but
                  System.out.println("str1 == str2: " + (str1 == str2));
                  • 21. Re: strings
                    807600
                    oops forgot operator precedence order, my mistake. ty
                    1 2 Previous Next